[英]javascript: convert BASE64 to BLOB fails in safari only
I'm trying to convert a blob (created with zip.js ) to a base64 and persist it in the websql database. 我正在尝试将blob(使用zip.js创建)转换为base64并将其保存在websql数据库中。 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) ;
});
see a working example: http://jsfiddle.net/jeanluca/4bn5G/ 参见工作示例: http : //jsfiddle.net/jeanluca/4bn5G/
So, the strange thing is, that it works in Chrome, but not in Safari (als not on my iPad). 因此,奇怪的是,它可以在Chrome中使用,但不能在Safari中使用(Always在我的iPad上也不可用)。
I also tried to rewrite the base64ToBlob to 我还尝试将base64ToBlob重写为
function base64ToBlob(base64) {
var binary = atob(base64);
return new Blob([binary]) ;
}
But then de uncompress doesn't work anymore, giving me an "IndexSizeError: DOM Exception 1 " exception 但是随后解压缩不再起作用,给了我一个“ IndexSizeError:DOM Exception 1”异常
Any suggestion what might be wrong in my code ? 任何建议在我的代码中可能有什么问题吗?
Thnx 日Thnx
Well I found a solution just after posting my comment. 好吧,我发表评论后就找到了解决方案。
Instead of 代替
new Blob([data]);
do 做
new Blob([data.buffer]);
notice the addition of ".buffer" 注意“ .buffer”的添加
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.