简体   繁体   English

Python Flask - 接收图像作为帖子

[英]Python Flask - receive image as post

I'm having trouble receiving an image from a form on my flask python server.我无法从 flask python 服务器上的表单接收图像。

Here's the code of the html form:这是 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>

Here's the code of the server这是服务器的代码

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)

Here's the output of the console when I submit the form:这是我提交表单时控制台的 output:

"POST /upload HTTP/1.1" 405 -

Essentially, just shows that site can't be reached.本质上,只是表明无法访问该站点。 When I go to the url without submitting image the page works normally.当我 go 到 url 没有提交图像时,页面正常工作。 I have no clue what I'm doing wrong.我不知道我做错了什么。 All help is appreciated.感谢所有帮助。

Method type (POST) is missing in your route.您的路线中缺少方法类型 (POST)。 So it's giving 405, which is method not allowed .所以它给出了 405,这是method not allowed

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

Web applications use different HTTP methods when accessing URLs. Web 应用程序在访问 URL 时使用不同的 HTTP 方法。 You should familiarize yourself with the HTTP methods as you work with Flask.在使用 Flask 时,您应该熟悉 HTTP 方法。 By default, a route only answers to GET requests.默认情况下,路由只响应 GET 请求。 You can use the method's argument of the route() decorator to handle different HTTP methods.您可以使用 route() 装饰器的方法参数来处理不同的 HTTP 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM