Question

I have the following code for creating a .txt file in javascript:

var text_block = '';

text_block += "Variable 1:"+var1+"\r\n";
text_block += "Variable 2:"+var2+"\r\n";
text_block += "Variable 3:"+var3;

var zip = new JSZip();
zip.file("variables.txt", text_block);

It's going into a zip file because it will ultimately be packaged with other files. When I run this script, it's creating the text file, but there are no new lines/carriage returns when opened in notepad. They do show when opened with wordpad, but I can't assume people are going to use that by default for a .txt file. What can I do to show line breaks in notepad?

Was it helpful?

Solution

Looking at the source of JSZip, in the file jszip.js of the download package on their website, I noticed this line of code (line 661-662):

  utf8encode : function (string) {
     string = string.replace(/\r\n/g,"\n");

So it seems like that's your problem. Perhaps you could try commenting out line 662, I don't know why it's there, it may well break something else. It seems they copied the code from here, as per the url in the source.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top