Aly
Aly

Reputation: 16295

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?

Upvotes: 5

Views: 3300

Answers (1)

Aly
Aly

Reputation: 16295

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>

Upvotes: 9

Related Questions