Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New line character in output file

When I try to write a string with multiple lines to an output text file the newline chars are not preserved and all the content is printed on one single line.

In the specific I have a button with a listener on click with associated this 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 correctly downloaded, but string1, string2 and string3 are on the same line.

Any suggestion?

like image 828
Andrea Avatar asked Nov 02 '15 16:11

Andrea


People also ask

How do you add a new line in output?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

What is the character for New line?

LF (character : \n, Unicode : U+000A, ASCII : 10, hex : 0x0a): This is simply the '\n' character which we all know from our early programming days. This character is commonly known as the 'Line Feed' or 'Newline Character'.

How do you print a new line character?

The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.

Is EOF and \n same?

EOF is not same of null character \0 . I think you want to know whether you can get an EOF without getting a newline first. The answer is yes. It is possible if there is no newline at the end of file, i.e. when the last line finished without a newline at end.


1 Answers

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

Try this:

var textToSend = string1+"\r\n"+string2+"\r\n"+string3;
textToSend = encodeURIComponent(textToSend);
this.downloadButton.setAttribute('href', 'data:text/plain;charset=utf-8,' + textToSend)
like image 113
musefan Avatar answered Oct 04 '22 00:10

musefan