I'm making an AJAX call to a Django POST view. It's reading X files locally, and then sending back to the front-end (thanks to ajax), so the user can download them.
Everything is working fine on this part, but i'd like to have some sort of "intermediate response", which would tell Ajax each time the server reads one file.
That way, i could create a loading bar on the front end while the server is processing the request.
Is there any way i could do it, using ajax, or something else (anything would be fine, as long as i'd keep my ajax request somewhere).
Here is my code, quite simplified, so you can understand better what I'd like it to do:
Django:
def downloadFiles_API(request):
if request.is_ajax and request.method == "POST":
requested_files = json.loads(request.body)['files']
for file in requested_files:
# Handle file reading (this part works)
# Here i'd like to tell the front end "File X has been loaded..." (for each file)
# Zip-ing all files, and send it as response
return FileResponse(myZipFile)
AJAX:
$.ajax({
url: "/api/downloadFiles/",
type: 'POST',
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
"files" : filesThatTheUserWants,
}),
xhrFields:{
responseType: 'blob'
},
success: function (response){
// Download the file in response (this works)
},
// And what i'd like to add (it's NOT in my code):
onProgress: function (intermediateResponse){
console.log(intermediateReponse);
// Would print "File X has been loaded..."
// Each time a file is loaded on server side
}
Note: I found some similar questions, but they are either too generic, or not explaining the ajax part (like these: Send intermediate messages during an Ajax call, or How to stream an HttpResponse with Django)