javascript:将BASE64转换为BLOB仅在safari中失败 [英] javascript: convert BASE64 to BLOB fails in safari only
问题描述
我正在尝试转换blob(使用 zip.js 创建)到base64并将其保存在websql数据库中。然后我也想以相反的方式做这个过程。无论如何,我的测试代码(没有压缩)看起来像:
I'm trying to convert a blob (created with zip.js) to a base64 and persist it in the websql database. Then I would also like to do this process the other way around. Anyway, my test code (without the compression) looks something like:
var blob = new Blob([data], {
type : "text/plain"
});
blobToBase64(blob, function(b64) { // convert BLOB to BASE64
var newBlob = base64ToBlob(b64) ; // convert BASE64 to BLOB
console.log(blob.size + " != " + newBlob.size) ;
});
查看一个工作示例: http://jsfiddle.net/jeanluca/4bn5G/
所以,奇怪的是,它适用于Chrome,但不在Safari中(不在我的iPad上)。
So, the strange thing is, that it works in Chrome, but not in Safari (als not on my iPad).
我还尝试将base64ToBlob重写为
I also tried to rewrite the base64ToBlob to
function base64ToBlob(base64) {
var binary = atob(base64);
return new Blob([binary]) ;
}
然后de uncompress不再起作用,给我一个IndexSizeError: DOM例外1例外
But then de uncompress doesn't work anymore, giving me an "IndexSizeError: DOM Exception 1 " exception
我的代码中有什么可能出错的建议吗?
Any suggestion what might be wrong in my code ?
Thnx
推荐答案
我发布评论后发现了一个解决方案。
Well I found a solution just after posting my comment.
而不是
new Blob([data]);
do
new Blob([data.buffer]);
注意添加.buffer
notice the addition of ".buffer"
这篇关于javascript:将BASE64转换为BLOB仅在safari中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!