Home >Web Front-end >JS Tutorial >Can I Predefine a Suggested Filename for Data URI Downloads?

Can I Predefine a Suggested Filename for Data URI Downloads?

DDD
DDDOriginal
2024-12-02 08:12:14507browse

Can I Predefine a Suggested Filename for Data URI Downloads?

Specifying a Suggested Filename for data: URI Downloads

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn