Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask request.files is empty [duplicate]

Tags:

python

flask

much like this question, I'm trying to follow the simple Flask tutorial for file upload to a flask server. In my specific case, I'm trying to upload an XML file.

The (simplified) HTML I'm using is:

<form action="" method="post" enctype="multipart/form-data">
    <input type="file">
    <input type="submit" value="Let's go!">
</form>

The request is correctly handled by a if request.method == 'POST': block, so I put in some print statements to troubleshoot:

print('request.method', request.method)
print('request.args', request.args)
print('request.form', request.form)
print('request.files', request.files)

and the result was the following:

request.method POST
request.args ImmutableMultiDict([])
request.form ImmutableMultiDict([])
request.files ImmutableMultiDict([])

What am I doing wrong? I can provide more complete source code if needed.

like image 371
Cass Avatar asked Apr 30 '18 22:04

Cass


1 Answers

As always, I found the answer mere minutes after posting this question. I'm answering here to hopefully help someone else.

The problem was that my file input had no name attribute. Thanks to Ben here I was able to fix this problem by adding a name attribute to the file input, and now the file upload is being processed correctly.

like image 142
Cass Avatar answered Nov 02 '22 14:11

Cass