Download attribute, new line in text file

2

I'm trying to download a text file with information coming from a array but I would like to separate these values into a new line ... I've tried some bad approaches, but the result remains in only one line.

let codes = [
    'sajkhasjdgsa456456',
    'hjkgd576k3nt67sdffd',
    'kjhkdgyfg8677dsfbjk',
    'mcvsaud423409878gsad',
    '123786dhjfsd734234ds45'
]

$('#download-link').on('click', function(code) {
   let data = ''
   for (let i = 0; i < codes.length; i++) {
        data += codes[i] + '\n'
   }
   $(this).attr('href', 'data:text/plain;charset=utf-8,' + data)
})

$('#download-link2').on('click', function(code) {
   let data = codes.join('\n')
   $(this).attr('href', 'data:text/plain;charset=utf-8,' + data)
})
<a id="download-link" href="" download="codes.txt">baixar</a>
<br>
<a id="download-link2" href="" download="codes-2.txt">baixar 2</a>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
asked by anonymous 27.09.2017 / 18:17

2 answers

3

Change \n by %0D%0A that will work. Source: answer in StackOverflow in English .

    
27.09.2017 / 18:59
6

Just use escape (since there are no accents or anything), it should look like this:

  

If you have to work with accents or have spaces or the + b signal might have to use encodeURI or encodeURIComponent instead of escape , in your example as it only has "random" characters like AZ and 0-9 escape should work fine

let codes = [
    'sajkhasjdgsa456456',
    'hjkgd576k3nt67sdffd',
    'kjhkdgyfg8677dsfbjk',
    'mcvsaud423409878gsad',
    '123786dhjfsd734234ds45'
]

$('#download-link').on('click', function(code) {
   let data = ''
   for (let i = 0; i < codes.length; i++) {
        data += codes[i] + '\n'
   }

   data = escape(data);

   $(this).attr('href', 'data:text/plain;charset=utf-8,' + data)
})

$('#download-link2').on('click', function(code) {
   let data = codes.join('\n');

   data = escape(data);

   $(this).attr('href', 'data:text/plain;charset=utf-8,' + data)
})
<a id="download-link" href="" download="codes.txt">baixar</a>
<br>
<a id="download-link2" href="" download="codes-2.txt">baixar 2</a>


<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Line break in "Notepad" (notepad.exe)

Line wrapping \n does not work in some programs with notepad.exe of Windows, this may be because of some need with backwards compatibility

For the document to be visible in notepad.exe you should change \n to \r\n , then to explain:

  • \n is the line feed (LF)
  • \r is carriage return (CR)

LF was used as line break in Linux and Unix-based systems, CR was used by Mac systems (before OSX), while CRLF was used by older Microsoft systems, however I believe Notepad.exe still uses LF to be compatible with old files and of course we have software that replaces traditional notepad in Windows (such as Notepad ++, SublimeText , etc), so if you want to keep a backward compatibility change in your code the CRLF by .join('\n')

Example:

let codes = [
    'sajkhasjdgsa456456',
    'hjkgd576k3nt67sdffd',
    'kjhkdgyfg8677dsfbjk',
    'mcvsaud423409878gsad',
    '123786dhjfsd734234ds45'
]

$('#download-link').on('click', function(code) {
   let data = ''
   for (let i = 0; i < codes.length; i++) {
        data += codes[i] + '\r\n'
   }

   data = escape(data);

   $(this).attr('href', 'data:text/plain;charset=utf-8,' + data)
})

$('#download-link2').on('click', function(code) {
   let data = codes.join('\r\n');

   data = escape(data);

   $(this).attr('href', 'data:text/plain;charset=utf-8,' + data)
})
<a id="download-link" href="" download="codes.txt">baixar</a>
<br>
<a id="download-link2" href="" download="codes-2.txt">baixar 2</a>


<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
27.09.2017 / 19:04