New line character in the output file

When I try to write a line with several lines to the output text file, newline characters are not saved and all the contents are printed on one line.

In specific, I have a button with a listener on click with the corresponding function:

function (e) { this.downloadButton.setAttribute("download", "output.txt"); var textToSend = string1+"\r\n"+string2+"\r\n"+string3; this.downloadButton.setAttribute('href', 'data:text/plain;charset=utf-8,' + textToSend); } 

The file is loaded correctly, but string1, string2 and string3 are on the same line.

Any suggestion?

+6
source share
2 answers

I think you may need to encode your data, which you can do with encodeURIComponent() .

Try the following:

 var textToSend = string1+"\r\n"+string2+"\r\n"+string3; textToSend = encodeURIComponent(textToSend); this.downloadButton.setAttribute('href', 'data:text/plain;charset=utf-8,' + textToSend) 
+4
source

Use encodeURIComponent() . See a working example below.

 var downloadButton = document.getElementById('download'); var textToSend = encodeURIComponent("string1\r\nstring2\r\nstring3"); downloadButton.setAttribute('href', 'data:text/plain;charset=utf-8,' + textToSend); 
 <a id="download" download="output.txt">Download</a> 
+3
source

All Articles