Reputation: 7692
I need to download files that are saved as base64
string. The file can be of any type.
I've tried to make window.open(prefix + base64);
where prefix is a string that JS Reader generates when reading asDataUrl
. However, downloading file with prefix data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,
resulted in file that has no extension.
What can be done here? (I have to use base64 as a source)
Upvotes: 2
Views: 4619
Reputation: 224906
You can make an <a download>
in JavaScript and click it to suggest a filename:
document.getElementById('download').onclick = () => {
const base64 = ''; // …
const link = document.createElement('a');
link.href = 'data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,' + base64;
link.download = 'my-document.docx';
link.click();
};
<button type="button" id="download">Download</button>
Although I don’t know which browsers don’t recognize that media type as .docx, so maybe they don’t support the download
attribute either.
Upvotes: 3