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

268
Visualizações
How do I download a file in javascript without disconnecting web-sockets or flashing a new tab?

After a long running server job, I need to automatically trigger a file download in my user's browser. I also have active websocket connections that I need to maintain in case the user is preforming different tasks in my web app. Originally I was doing this:

window.location.replace(downloadLink);

However, after that line executed I would miss websocket messages. This question deals with the same problem: On-Click A-tag with Href, socket gets disconnected

Then I tried this:

window.open(downloadLink, "_blank");

However this flashes a new tab open and closes it immediately. Its very jarring if the user was working on something else. I have also tried this:

axios.get(downloadLink, { responseType: "blob" }).then((response) => {
  const fileURL = window.URL.createObjectURL(new Blob([response.data]));
  const docUrl = document.createElement("a");
  docUrl.href = fileURL;
  docUrl.setAttribute("download", fileName);
  document.body.appendChild(docUrl);
  docUrl.click();
});

This works but it downloads the file in the background. This file is very large so I would like the user to be able to see download progress in the downloads bar at the bottom of the page.

Does anyone know how to download a file without disconnecting web-sockets or opening a new tab while still displaying the browsers file download progress?

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