I'm trying to create a text file with one string (called 'summary') as the only content. summary consists of many different lines, so I use '\n' quite a lot. However, the text file that gets created writes the string in a single line. Why do the '\n's get ignored?
This is the function I'm using:
function writeToTxt(summary){
var fileType = "text/txt";
var fileName = Date.now()+".txt";
var blob = new Blob([summary], { type: fileType });
var a = document.createElement('a');
a.download = fileName;
a.href = URL.createObjectURL(blob);
a.dataset.downloadurl = [fileType, a.download, a.href].join(':');
a.style.display = "none";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
setTimeout(function() { URL.revokeObjectURL(a.href); }, 1500);
}
Thanks!
\nis a line break on *nix, but not on Windows. If you're looking at the file on Windows, that's probably why you're not seeing line breaks. For Windows it's\r\n.\nas part of a JavaScript string literal. Anything else (e.g. reading it from a text file) will not get it parsed.