There is no ideal way to do this only on the client side. In the browsers that support data URIs , you can do this:
function downloadFile(conteudo) {
window.location.href = 'data:application/octet-stream;charset=utf-8,' + encodeURIComponent(conteudo);
}
Demo
The problem with this is that the browser will decide the name of the file that will be downloaded. There is a clue to this, but it will only work on browsers that support the download
attribute on 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 with no compatibility issues, pass the content to the server and force the download from there.