简体   繁体   English

来自javascript二进制字符串的Blob

[英]Blob from javascript binary string

I have a binary string created with FileReader.readAsBinaryString(blob). 我有一个用FileReader.readAsBinaryString(blob).创建的二进制字符串FileReader.readAsBinaryString(blob).

I want to create a Blob with the binary data represented in this binary string. 我想用这个二进制字符串表示的二进制数据创建一个Blob。

Is the blob that you used not available for use anymore? 您使用的blob不再可用吗?
Do you have to use readAsBinaryString ? 你必须使用readAsBinaryString吗? Can you use readAsArrayBuffer instead. 你可以使用readAsArrayBuffer With an array buffer it would be much easier to recreate the blob. 使用数组缓冲区,重建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"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM