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
Release: 2024-12-02 08:12:14
Original
448 people have browsed it

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=">
Copy after login

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();
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template