Flask gives error 400 when uploading file

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 webapp flash drive, I have the following rule:

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

But I get error 400: bad requestthat from my googling tells me that Flask cannot find the file under the key 'imagefile', which is the input name in html. Any ideas why this doesn't find it?

+4
source share
1 answer

Turns out I need to include enctypein 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>
+6
source

All Articles