Skip to content

Instantly share code, notes, and snippets.

@parkerproject
Forked from danallison/downloadString.js
Created June 29, 2020 03:22
Show Gist options
  • Save parkerproject/d2ea78fee6612c1f2720df9c43cc2f83 to your computer and use it in GitHub Desktop.
Save parkerproject/d2ea78fee6612c1f2720df9c43cc2f83 to your computer and use it in GitHub Desktop.
download string as text file
function downloadString(text, fileType, fileName) {
var blob = new Blob([text], { type: fileType });
var a = document.createElement('a');
a.download = fileName;
a.href = URL.createObjectURL(blob);
a.dataset.downloadurl = [fileType, a.download, a.href].join(':');
a.style.display = "none";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
setTimeout(function() { URL.revokeObjectURL(a.href); }, 1500);
}
// downloadString("a,b,c\n1,2,3", "text/csv", "myCSV.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment