I'm having problems with ajax and flask connection when sending a FormData in a request.
I'm trying to send an array of files from javascript with ajax:
dropArea.addEventListener("drop", (event)=>{
event.preventDefault(); //preventing from default behaviour
file = getDroppedOrSelectedFiles(event);
file.then((value) => {
res = []
for (var v in value) {
console.log(value[v].fileObject)
res.push(value[v].fileObject)
}
var fd = new FormData();
fd.append("file[]", res.serialize())
for (var value of fd.values()) {
console.log(value);
}
var req = {
url: "/upload.html",
method: "post",
processData: false,
data : fd
};
var promise = $.ajax(req);
});
});
And just read this list of files in flask. I've tried with files = request.form.getlist("files[]")
but I only receive an empty list. Investigating I've discovered that if I convert request.form
into a dictionary the files are actually there but neither the key corresponds to "file[]" nor the values to the files.
dict_items([('-----------------------------110480676136716695933348531592\r\nContent-Disposition: form-data; name', '"file[]"\r\n\r\n[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],[object File],,[object File]\r\n-----------------------------110480676136716695933348531592--\r\n')])
Does anyone know how can I solve it and obtain the list of files?