来自 javascript 二进制字符串的 Blob
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27810163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Blob from javascript binary string
提问by Will Hardwick-Smith
I have a binary string created with FileReader.readAsBinaryString(blob).
我有一个创建的二进制字符串 FileReader.readAsBinaryString(blob).
I want to create a Blob with the binary data represented in this binary string.
我想用这个二进制字符串中表示的二进制数据创建一个 Blob。
回答by Musa
Is the blob that you used not available for use anymore?
Do you have to use readAsBinaryString
? Can you use readAsArrayBuffer
instead. With an array buffer it would be much easier to recreate the blob.
您使用的 blob 是否不再可用?
你必须使用readAsBinaryString
吗?可以readAsArrayBuffer
代替吗。使用数组缓冲区,重新创建 blob 会容易得多。
If not you could build back the blob by cycling through the string and building a byte array then creating a blob from it.
如果不是,您可以通过循环遍历字符串并构建一个字节数组然后从中创建一个 blob 来构建回 blob。
$('input').change(function(){
var frb = new FileReader();
frb.onload = function(){
var i, l, d, array;
d = this.result;
l = d.length;
array = new Uint8Array(l);
for (var i = 0; i < l; i++){
array[i] = d.charCodeAt(i);
}
var b = new Blob([array], {type: 'application/octet-stream'});
window.location.href = URL.createObjectURL(b);
};
frb.readAsBinaryString(this.files[0]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="file">