There is no ideal way to do this only on the client side. We browsers that support data Uris, you can do so:
function downloadFile(conteudo) {
window.location.href = 'data:application/octet-stream;charset=utf-8,' + encodeURIComponent(conteudo);
}
Demo
The problem is that the browser will decide the name of the file that will be downloaded. It has a gambit for this, but will only work in browsers that support the attribute download
in anchors:
function downloadFile(conteudo, filename) {
var ancora = document.createElement('a');
ancora.href = 'data:application/octet-stream;charset=utf-8,' + encodeURIComponent(conteudo);
ancora.download = filename;
ancora.click();
}
Demo
If you want a solution without compatibility issues, pass the content to the server and force the download from there.
Can explain better the relationship between/textarea and JSON?
– Sergio
It might be better to include your current code, both involving the textarea and the expected JSON.
– dang
"read carefully"... What, the two lines of explanation? I saw the summary on the main page and opened the question waiting for at least 4 paragraphs...
– brasofilo
I’m sorry, but it’s really hard to understand I tried to explain to some and they didn’t understand... It is as follows: I already have the code that generates the JSON text that is in the textarea only need to download it as a file . json.
– Júnior