javascript: convert BASE64 to BLOB fails in safari only

前提是你 提交于 2019-12-14 03:43:01

问题


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) ;
});

see a working example: http://jsfiddle.net/jeanluca/4bn5G/

So, the strange thing is, that it works in Chrome, but not in Safari (als not on my iPad).

I also tried to rewrite the base64ToBlob to

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

Any suggestion what might be wrong in my code ?

Thnx


回答1:


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"



来源:https://stackoverflow.com/questions/17924501/javascript-convert-base64-to-blob-fails-in-safari-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!