in my web app, I want to provide a download for a message log. This is done by the following javascript function:
let download = (content, filename) => {
let uriContent = "data:application/octet-stream;charset=utf-8;base64," + btoa(content);
let link = document.createElement('a');
link.setAttribute('href', uriContent);
link.setAttribute('download', filename);
let event = new MouseEvent('click');
link.dispatchEvent(event);
};
The function is executed by a mouse click and works fine in most cases.
However, when the file becomes a little bigger ( > ~3MB), the download fails in Chrome with a network error.
The same download works fine in other browsers like Firefox. Incognito mode did not solve the issue.
Can I change something in my code to make the download work? Or is this some bug (or feature) of Chrome that prevents the download?
There is a limit of (slightly less than) 2MB. See https://bugs.chromium.org/p/chromium/issues/detail?id=69227
You can workaround it using blob urls. https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL
let download = (content, filename) => {
let uriContent = URL.createObjectURL(new Blob([content], {type : 'text/plain'}));
let link = document.createElement('a');
link.setAttribute('href', uriContent);
link.setAttribute('download', filename);
let event = new MouseEvent('click');
link.dispatchEvent(event);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With