Flask在文件上传时给出错误400

问题描述:

我有以下几种:

I have the following

<form action="classify_upload" method="post" id="upload-form">
    <input type="file" name="imagefile" id="imagefile"/>
    <input type="submit" />
</form>

在我的烧瓶webapp中,我有以下规则:

And in my flask webapp I have the following rule:

@webapp.route('/upload', methods=['POST'])
def upload():
    try:
        imagefile = flask.request.files['imagefile']
        ...
    except Exception as err:
        ...

但是我得到一个 400错误:坏请求,从我的谷歌搜索告诉我Flask找不到文件下的'imagefile'这是html中输入的名字。任何想法,为什么它没有找到它?

But I am getting a error 400: bad request, which from my googling tells me Flask can not find the file under the key 'imagefile' which is the name of the input in the html. Any ideas why it is not finding it?

原来我需要包括 enctype ,所以html应该是

Turns out I need to include the enctype in the form, so the html should be

<form action="classify_upload" method="post" id="upload-form"  enctype="multipart/form-data">
    <input type="file" name="imagefile" id="imagefile"/>
    <input type="submit" />
</form>