2

I'm using ajax to send file via POST request to the server (my Flask app), but I can only retrieve this file (binary data) in Flask using request.form, not request.files, and request.files is desired. Code as below.

How can I get file with request.files ? (Conversely, with Postman and other tools, I can get the file with request.files. In request.form case, info such as content-type is missing)

// "file_content" is read previously

var formData = new FormData();
formData.append("file", file_content);  // "file_content" is binary data

.ajax({
  type:"POST",
  async: false,
  url: ...,
  data: formData,
  dataType:"json",
  processData: false,
  contentType: false,
});

The tricky thing is send a local file, and most relevant answers are using document.getElementById().

frogcdcn
  • 391
  • 1
  • 4
  • 14
  • please share with us the code of view function that handles the `POST` request to retrieve file. maybe this tutorial helps you https://pythonbasics.org/flask-upload-file/, i guess you are inspiring code from https://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax, also make sure to add `enctype = "multipart/form-data"` in your form – cizario Jun 28 '20 at 08:44

0 Answers0