Function that generates file

Asked

Viewed 179 times

2

How to create a javascript function that generates a.ini file?

For example, I will fill in 2 fields and it will generate a file like this

[teste]
nome: "campo 1 que vou preencher"
teste: teste
teste: teste
email: "campo 2 que vou preencher"
teste: teste
teste:

That would be it, then the person clicks on Save or Download and downloads the file I’m trying to use Filesaver, but I’m having trouble even to run his demo

I tried in another way too more or less like this

$('#salvar').click(function() {
    var nome = $('#nome').val();
    var email = $('#email').val();
    this.href = "data:text/plain;charset=UTF-8," + encodeURIComponent(texto);
});
  • You want to generate content in Javascript and make it so that when the user clicks a download with a file starts, that’s it?

  • This, for example, the user fills two fields and when clicking on Save/Download, the.ini file will be downloaded to him with the default file data + the data he fills in

  • The tag node.js does not apply to this question.

2 answers

4

function download(filename, nome, email) {
  var element = document.createElement('a')
  element['href'] = 'data:text/plain;charset=utf-8,' + encodeURIComponent(nome + '\n' + email)
  element['download'] = filename

  element['style']['display'] = 'none'
  document['body']['appendChild'](element)

  element['click']()

  document['body']['removeChild'](element)
}
form * {
  display: block;
  margin: 10px;
}
<form onsubmit="download(this['arquivo'].value, this['nome'].value, this['email'].value)">
  <input type="text" id="arquivo" value="teste.txt">
  <input type="text" id="nome" name="Nome">
  <input type="text" id="email" name="E-Mail">	    
  <input type="submit" value="Download">
</form>

Source: https://stackoverflow.com/a/18197341/1997073

  • @nicematt feel free to edit the answer.

  • 1

    Thank you so much for your reply too, helped me in the same way, I just got a little more confused to create the file template

  • @John +1 for feedback! :)

4


A different approach would be to create a blob with the values. After that, just download.

An example of the use would be thus:

function baixarArquivo() {
  var nome = document.getElementById('nome').value;
  var email = document.getElementById('email').value;

  var texto = '[teste]\n' +
    'nome: ' + nome + '\n' +
    'teste: teste\n' +
    'teste: teste\n' +
    'email: ' + email + '\n' +
    'teste: teste\n' +
    'teste:';

  var fileName = 'arquivo.txt';
  var fileContent = texto;

  var arquivo = new Blob([fileContent], {
    type: 'text/plain'
  });

  window.URL = window.URL || window.webkitURL;
  document.getElementById('download').setAttribute('href', window.URL.createObjectURL(arquivo));
  document.getElementById('download').setAttribute('download', fileName);
}
Nome: <input id="nome" />
<br/>
E-mai: <input id="email" />
<br/>
<a href="" id="download" onclick="baixarArquivo();">Download</a>

Remembering that you can change the extension in the file name.

Reference

  • I wanted to better understand the first parameter of Blob. Why did you declare an object in it? Anyway, it seems a good alternative

  • 1

    @nicematt The first parameter is a Array and derivatives. What is accepted you can see better in the documentation. If you like, I will explain in more detail in the reply later.

  • Thank you very much, that’s what I was trying to understand, now I will try to make the file come in the format of the model I spoke. grateful!

  • Didn’t need setAttribute to define HTMLElement().download and HTMLElement().href, right

Browser other questions tagged

You are not signed in. Login or sign up in order to post.