I'm trying to save my HTML file in Chrome when the user presses ctrl + s
keys but Chrome is crashed.
(I want to download just the source code of my HTML file)
I read that it happens because my file is bigger than 1.99M..
In the first attempt (before I knew about the crashing in Chrome):
function download(filename, text) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
pom.click();
}
download('test.html', "<html>" + document.getElementsByTagName('html')[0].innerHTML + "</html>");
The second attempt, after I read about the crashing, I used blob
:
function dataURItoBlob(dataURI) {
var byteString = atob(dataURI.split(',')[1]);
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
var bb = new BlobBuilder();
bb.append(ab);
return bb.getBlob(mimeString);
}
function download(dataURI) {
var blob = dataURItoBlob(dataURI);
var url = window.URL.createObjectURL(blob);
window.location.assign(url);
}
download("<html>" + document.getElementsByTagName('html')[0].innerHTML + "</html>")
Here I got the error: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.
I don't know, but I read that I need to encode my string to base64: How can you encode a string to Base64 in JavaScript?
There is an answer of 148 votes. I paste it in my code and don't know how to continue.
Where should I call it and how? Can I put a name on my saved file?
I think that I need to do something like:
download(_utf8_decode("<html>" + document.getElementsByTagName('html')[0].innerHTML + "</html>"))