16

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.

cass
  • 858
  • 1
  • 10
  • 17

1 Answers1

32

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.

cass
  • 858
  • 1
  • 10
  • 17
  • 33
    For anyone else who is directed to this question through searching, `request.files` will also return empty if you don't set `enctype="multipart/form-data"` on the form (which you did). I know this doesn't apply to your specific situation, which is why I'm leaving it as a comment and not an answer, but it's another good reason for `request.files` to appear empty. – rubynorails Jul 08 '20 at 20:55