Flask在文件上传时给出错误400 [英] Flask giving error 400 on file upload

查看:617
本文介绍了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>

这篇关于Flask在文件上传时给出错误400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆