Skip to content
Advertisement

How to upload a file using an ajax call in flask

Hi I’m quite new to flask and I want to upload a file using an ajax call to the server. As mentioned in the documentation, I added a file upload to the html as folows:

JavaScript

and I wrote the ajax handler as this

JavaScript

I do not know how to get the uploaded file (not the name) from this

JavaScript

and save the file in folder. I’m not quite sure how to read the file from handler which i have written:

JavaScript

I will be grateful if anyone can help. Thank you in advance

Advertisement

Answer

To answer your question…

HTML:

JavaScript

JavaScript:

JavaScript

Now in your flask’s endpoint view function, you can access the file’s data via flask.request.files.

On a side note, forms are not tabular data, therefore they do not belong in a table. Instead, you should resort to an unordered list, or a definition list.

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