Python Flask-接收图像作为发布

我无法从烧瓶python服务器上的表单接收图像。

这是html表单的代码:

<form action="http://localhost:85/upload" method="POST" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="myImage" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

这是服务器的代码

from flask import flask,request

app = flask(__name__)

@app.route("/")
def main():
    return("Welcome!")

@app.route('/upload')
def upload():
    try:
        # check if the post request has the file part
        file = request.files['myImage']
        return("Image uploaded")
        print("Image uploaded")
    except Exception as err:
        print("Error occurred")
        print(err)
        return("Error,image not received.")

if __name__ == "__main__":
    app.run(debug=True,host="0.0.0.0",port=85)

这是我提交表单时控制台的输出:

"POST /upload HTTP/1.1" 405 -

从本质上讲,仅表明无法访问该网站。当我不提交图像而进入URL时,页面正常运行。 我不知道我在做什么错。感谢所有帮助。

hustsky123 回答:Python Flask-接收图像作为发布

您的路线中缺少方法类型(POST)。因此它给出405,即method not allowed

https://flask.palletsprojects.com/en/1.1.x/quickstart/#routing

Web应用程序在访问URL时使用不同的HTTP方法。在使用Flask时,您应该熟悉HTTP方法。默认情况下,路由仅回答GET请求。您可以使用route()装饰器的方法参数来处理不同的HTTP方法。

本文链接:https://www.f2er.com/3127758.html

大家都在问