How to create a text file with javascript? [closed]

4

I need to create a javascript text file that works for all browsers.

    
asked by Angel Enrique Herrera Crespo 30.06.2016 в 18:18
source

1 answer

13

You can use an anchor (element <a> ), and specify the href with a data URI that contains the content to be downloaded and use the download attribute to specify the file name.

Unfortunately you can not specify where the file will be stored, or even if it is stored, it depends on the user's decision.

var contenidoDeArchivo = "Hola Mundo!";
var elem = document.getElementById('descargar');

elem.download = "archivo.txt";
elem.href = "data:application/octet-stream," 
                     + encodeURIComponent(contenidoDeArchivo);
<a id="descargar">descarga</a>

In addition, there is an API called FileSystem API but it is chrome-specific, so it does not It works for your case.

    
answered by 30.06.2016 в 18:39