Skip to content
Advertisement

Posting file upload via AJAX returns empty dictionary on Flask side

I have a Flask app that was working until I added login functionality. The issue I’m having is I have a modal that the user can upload a file with.

HTML

JavaScript

Then an AJAX call to upload the file:

JavaScript

And finally the Flask code I’m using to debug:

JavaScript

The command line output on the python side is:

JavaScript

So it’s returning an empty dictionary. Previously, when I used:

JavaScript

It worked, but now with that code included it throws a 400 error because the 'exampleInputFile' is invalid key in the request.files dictionary. I removed the @login_required from the route and it didn’t change anything.

Advertisement

Answer

From my point of view, it is difficult to see why your code is not working. In your example, the form tag is not closed. Also, you didn’t describe how your javascript code is executed. Although I assume that you are using a click event, I tested your code with a submit event. I have come to the conclusion that the code works.

Here is the variant I used for testing:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement