Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask giving error 400 on file upload

I have the following

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

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:
        ...

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?

like image 551
Aly Avatar asked Jan 19 '15 16:01

Aly


1 Answers

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>
like image 142
Aly Avatar answered Oct 21 '22 06:10

Aly