Is there any way to specify a suggested filename when using data: URI? Is there any way to specify a suggested filename when using data: URI? javascript javascript

Is there any way to specify a suggested filename when using data: URI?


Use the download attribute:

<a download='FileName' href='your_url'>

The download attribute works on Chrome, Firefox, Edge, Opera, desktop Safari 10+, iOS Safari 13+, and not IE11.


Chrome makes this very simple these days:

function saveContent(fileContents, fileName){    var link = document.createElement('a');    link.download = fileName;    link.href = 'data:,' + fileContents;    link.click();}


HTML only: use the download attribute:

<a download="logo.gif" href="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7">Download transparent png</a>