Download data URL file Download data URL file javascript javascript

Download data URL file


If you also want to give a suggested name to the file (instead of the default 'download') you can use the following in Chrome, Firefox and some IE versions:

function downloadURI(uri, name) {  var link = document.createElement("a");  link.download = name;  link.href = uri;  document.body.appendChild(link);  link.click();  document.body.removeChild(link);  delete link;}

And the following example shows it's use:

downloadURI("data:text/html,HelloWorld!", "helloWorld.txt");


function download(dataurl, filename) {  const link = document.createElement("a");  link.href = dataurl;  link.download = filename;  link.click();}download("data:text/html,HelloWorld!", "helloWorld.txt");