Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

javascript - Blob constructor not working in safari / opera?

I'm trying to construct a Blob from an array buffer that original comes from a binary string. It works fine in Firefox & Chrome, but I don't know whats wrong with Safari & Opera

This is a simplified version of my problem: http://plnkr.co/edit/sfEEHf?p=preview

// 1x1 red PNG pixle
base64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQIW2P8z8DwHwAFBQIAHl6u2QAAAABJRU5ErkJggg==";
byteString = atob(base64);

// convert binary to array buff so we can construct a blob later
arrayBuffer = new ArrayBuffer(byteString.length);
intArray = new Uint8Array(arrayBuffer);

for (i = 0; i < byteString.length; i += 1) {
  intArray[i] = byteString.charCodeAt(i);
}

// construct blob
blob = new Blob([intArray], {type: "image/png"});
console.log(blob.size); // suppose to be 70 (its 19 in safari)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Safari you need to use the 'buffer' property on the TypedArray, i.e. this:

blob = new Blob([intArray.buffer], {type: "image/png"});

and it'll work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...