Skip to content

Instantly share code, notes, and snippets.

@formigone
Created April 23, 2015 12:25
Show Gist options
  • Save formigone/c25191d203b66cd81a6c to your computer and use it in GitHub Desktop.
Save formigone/c25191d203b66cd81a6c to your computer and use it in GitHub Desktop.
Save data from JavaScript to client
// This snippet came from some website and is not original
function saveTextAsFile()
{
var textToWrite = 'lorem ipsum';
var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
var fileNameToSaveAs = 'html5-blob-to-file.txt'
var downloadLink = document.createElement('a');
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
// Firefox requires the link to be added to the DOM before it can be clicked.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment