-2
function download(filename, text) {   var element = document.createElement('a');   element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));   element.setAttribute('download', filename);

  element.style.display = 'none';   document.body.appendChild(element);

  element.click();

  document.body.removeChild(element); }

How can I convert the linebreaks so that they show in the *.txt file?

Sarabjit Singh
  • 611
  • 7
  • 17
ThePat02
  • 27
  • 1
  • 1
  • 6

1 Answers1

1

You're not really giving us much to go on, but here goes:

Assuming that you're currently formatting the carriage returns with the escaped \n character format and not \r\n format and that the variable text is already declared and has the string you want to download to file.

The further assumption is that you're trying to view your downloaded file on Windows Notepad (when using notepad++ the text shows as you wish with carriage returns showing) this can be fixed by using the following line before the download function:

var text = text.replace(/\n/gmi, '\r\n');

Bear in mind I've little to go on other than you're download function so apologies for the vague answer. If you gave us a sanitised example of the function that gives us the string text we'd be able to pinpoint the issue.

See the following post for \r, \n & \r\n in greater detail: \r\n, \r and \n what is the difference between them?

Beavatron Prime
  • 309
  • 2
  • 4