Blob from javascript binary string

别来无恙 提交于 2019-11-27 02:53:00

问题


I have a binary string created with FileReader.readAsBinaryString(blob).

I want to create a Blob with the binary data represented in this binary string.


回答1:


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.

If not you could build back the blob by cycling through the string and building a byte array then creating a blob from it.

$('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">


来源:https://stackoverflow.com/questions/27810163/blob-from-javascript-binary-string

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