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

257
Visualizações
How can I download a .txt file from url?

I have a function to download files:

downloadFile(url, filename) {
    var link = document.createElement('a');
    link.href = url;
    link.download = filename;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    link = null;
},

This function work with .csv and .xlsx files. When a user click to a button .txt file supposed to download too. If it is a txt file nothing happens.

I add this this two lines to code;

    if(url.includes(".txt")){
      link.target = '_blank';
    }

It looks like this;

downloadFile(url, filename) {
    var link = document.createElement('a');
    link.href = url;
    link.download = filename;
    if(url.includes(".txt")){
      link.target = '_blank';
    }
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    link = null;
},

When a user want to download the txt file it opens on a tab of browser and they need to right click and save the file over and over again.

For example this link; Example txt file

So how can I make the URL ending with .txt extension downloadable?

This solution tried but it is not link, it just content. Not Work

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