Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

338
Visualizações
Send messages from Django to Ajax while processing the request

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)

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda