I need a function that will download the string in a javascript variable into a text file(5-7MB). The function should work across browsers.
I've tried making such a download function, but it doesn't work on Chrome. FireFox opens the 'save' dialog box, but Chrome doesn't. Also, when I disable 'ask where to save file' in Chrome, it shows 'Failed - Network Error'. Since I'm doing everything locally, I don't see why this issue crops up.
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
This doesn't open any dialog box for saving a file on Chrome(Version 76.0.3809.132). Same issue for Microsoft Edge(44.17763.1.0). I want it to work on all browsers, or at least work on FireFox and Chrome.
Edit 1: The Chrome issue seems to persist on other computers, so it can't be something wrong with the browser. I would also prefer to make my own function rather than use other projects(company issues).
Edit 2: It seems that the anchor size is limited to 2MB in Chrome. Is there a workaround for this? There are some answers that recommend using BLOB, but I'm not familiar with the concept. Any explanation would be appreciated.