在'窗口'上执行'atob'失败 [英] Failed to execute 'atob' on 'Window'
问题描述
我试图在用户按下 ctrl + s
键但Chrome遭到崩溃时将我的HTML文件保存在Chrome中。
(我只想下载我的HTML文件的源代码)
我读过它是因为我的文件大于1.99M ..
第一次尝试(在我知道Chrome中发生崩溃之前):
函数下载(文件名,文本){
var pom = document.createElement('a');
pom.setAttribute('href','data:text / plain; charset = utf-8,'+ encodeURIComponent(text));
pom.setAttribute('download',filename);
pom.click(); ('html')+ document.getElementsByTagName('html')[0] .innerHTML +< / html>) ;
第二次尝试,在阅读崩溃后,我用 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);
返回bb.getBlob(mimeString);
}
函数下载(dataURI){
var blob = dataURItoBlob(dataURI);
var url = window.URL.createObjectURL(blob);
window.location.assign(url);
download(< html> + document.getElementsByTagName('html')[0] .innerHTML +< / html>)
code>
这里我得到了错误:在'Window'上执行'atob'失败:解码是不正确的编码。
我不知道,但我读到,我需要我的字符串编码为base64:如何在JavaScript中将字符串编码为Base64?
有148票的答案。我将它粘贴到我的代码中,不知道如何继续。
我应该在哪里调用它,以及如何调用?我可以在我保存的文件上放一个名字吗?
我认为我需要这样做:
<$ p
> 下载(_utf8_decode(< html>+ document.getElementsByTagName('html')[0] .innerHTML +< / html>))
BlobBuilder
已过时,请使用 Blob
构造函数来代替:
URL.createObjectURL(new Blob([/ * whatever content * /],{type:'text / plain'}));
这会返回一个blob URL,然后您可以在锚点的 href
。您还可以修改锚点的下载
属性来操纵文件名:
< a href =/ *在此分配网址* /id =linkdownload =whatever.txt>下载我< / a>
提琴。如果我没有记错,可信任的非用户启动的下载有任意的限制;因此,我们将坚持点击链接,该链接被视为足够用户启动:)
更新:实际上它很简单,可以节省电流文档的HTML!无论何时我们的交互式链接被点击,我们都会用相关的blob更新它的 href
。在执行点击绑定事件后,这就是将被导航到的下载URL!
$('#link')。 on('click',function(e){
this.href = URL.createObjectURL(
new Blob([document.documentElement.outerHTML],{type:'text / html'})
);
});
再次拨弄。
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>"))
BlobBuilder
is obsolete, use Blob
constructor instead:
URL.createObjectURL(new Blob([/*whatever content*/] , {type:'text/plain'}));
This returns a blob URL which you can then use in an anchor's href
. You can also modify an anchor's download
attribute to manipulate the file name:
<a href="/*assign url here*/" id="link" download="whatever.txt">download me</a>
Fiddled. If I recall correctly, there are arbitrary restrictions on trusted non-user initiated downloads; thus we'll stick with a link clicking which is seen as sufficiently user-initiated :)
Update: it's actually pretty trivial to save current document's html! Whenever our interactive link is clicked, we'll update its href
with a relevant blob. After executing the click-bound event, that's the download URL that will be navigated to!
$('#link').on('click', function(e){
this.href = URL.createObjectURL(
new Blob([document.documentElement.outerHTML] , {type:'text/html'})
);
});
这篇关于在'窗口'上执行'atob'失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!