The line breaks are in the generated file, but that doesn’t mean that the program you use to read the file understands what is there.
A file can be saved with three types of control characters representing the ending of a line in a text document:
CR
Common in Mac operating systems, comes from Carriage Return.
Encoding: 0x0D (13 decimal)
LF
Common in Unix operating systems, comes from Line Feed.
Encoding: 0x0A (10 decimal)
CR/LF
Common in Windows operating systems.
Any of the above line terminators is perfectly valid, but not all programs understand them, resulting in the contents of the file appearing on a single line.
Solution
The solution is to make use of the line terminator that suits us, where for this purpose, after reading the text to be saved, we manipulate it to standardize the results.
Since browsers use different line terminators from each other: \r
or \n
or \r\n
respectively, the most certain seems to be the appropriate line terminator usage for the user’s operating system.
So, there’s an example below for testing and performing a simple test of typing two or more lines of text and clicking a button.
What’s going to happen is:
- Read the value of
textarea
;
- Separate text for an array where each input is a line read, using a regular expression that looks for the 3 possible line terminators;
Merge text with the appropriate line terminator for the detected operating system:
We make use of the information provided by the property navigator.appVersion
and we try to locate an operating system indicator to return the corresponding line terminator:
function qualOS() {
var x = "\r\n";
if (navigator.appVersion.indexOf("Win") != -1) x = '\r\n';
if (navigator.appVersion.indexOf("Mac") != -1) x = '\r';
if (navigator.appVersion.indexOf("X11") != -1) x = '\n';
if (navigator.appVersion.indexOf("Linux") != -1) x = '\n';
return x;
}
Fire an alert to see the text.
Example
function qualOS() {
var x = "\r\n";
if (navigator.appVersion.indexOf("Win") != -1) x = '\r\n';
if (navigator.appVersion.indexOf("Mac") != -1) x = '\r';
if (navigator.appVersion.indexOf("X11") != -1) x = '\n';
if (navigator.appVersion.indexOf("Linux") != -1) x = '\n';
return x;
}
function testar() {
var texto = document.getElementById("texto").value;
var linhas = texto.split(/\r\n|\r|\n/g);
var textoFinal = linhas.join(qualOS());
alert(textoFinal);
}
textarea {
width: 400px;
height: 200px;
}
<textarea id="texto"></textarea>
<p>
<button onclick="testar()">testar</button>
</p>
This code, in your case, will be to be applied between reading the value of the textarea
and the generation of the blob
:
function saveTextAsFile() {
var textToWrite = document.getElementById("inputTextToSave").value;
// aqui, fazer aqui a verificação e conversão do terminado de linha
var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
// ...
}
Just a tip, if you right click on this page and on Inspect Element, at the end will have all the javascript code of the site, including the function you want :)
– Eduardo Silva
I already have the code in hand, is saving it =). the problem is in the generated file, it does not save the "skipped" lines, there is some hint to implement this?
– João
In my tests, line breaks are saved normally. You are using an input or textarea?
– Guilherme Nagatomo