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

153
Visualizações
Flask send_file as attachment the file download but ajax success function has no response

I'm creating a YouTube Video Downloader for which I use flask for the backend. I send an ajax request to the flask in return a flask return file which by default set to download. The file starts downloading but the problem is that there is no responseText or statusCode in the success function in ajax
Ajax Function which I used to work in the same way as jquery ajax does
My Ajax code is

    let formData = new FormData(downloadform);
    ajax({
      url: '/download',
      method: "POST",
      data: formData,
      onprogress: () => {
        download_loadingBtn.style.display = "block";
        downloadBtn.style.display = "none";
      },
      onsuccess: (responseText, status, statusText) => {
        download_loadingBtn.style.display = "none";
        downloadBtn.style.display = "block";
        console.log(responseText, status, statusText);
      },
      onfailure: (status, statusText) =>{
        console.log(status, statusText);
      }
    })

My Flask code is

@app.route("/download", methods = ["GET", "POST"])
def download_video():
    if request.method == "POST":
        buffer = BytesIO()
        url = YouTube(session['link'])
        itag = request.form.get("itag")
        video = url.streams.get_by_itag(itag)
        video.stream_to_buffer(buffer)
        buffer.seek(0)
        return send_file(buffer, as_attachment=True, download_name=url.title, mimetype="video/mp4") 

I also tried to return an object with some response text and send_file() such as

return jsonify({
       "text": "successfully downloaded",
       "download_file": send_file(buffer, as_attachment=True, download_name=url.title, mimetype="video/mp4") 
})

but that also gives an error i.e. TypeError: Object of type Response is not JSON serializable

about 4 years ago · Juan Pablo Isaza
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