When downloading a file from a data URI, browsers typically prompt the user to choose a filename. Is it possible to specify a suggested filename within the hyperlink?
In Markup
Yes, you can use the download attribute:
<a download="FileName" href="data:application/octet-stream;base64,SGVsbG8=">
The download attribute is supported by Chrome, Firefox, Edge, Opera, desktop Safari 10 , iOS Safari 13 , but not IE11.
In JavaScript
If the download attribute is not supported, you can use JavaScript to simulate the download and specify the filename:
const blob = new Blob(['Hello'], { type: 'text/plain' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'FileName.txt'; a.click();
The above is the detailed content of Can I Predefine a Suggested Filename for Data URI Downloads?. For more information, please follow other related articles on the PHP Chinese website!