Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

266
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda