来自 javascript 二进制字符串的 Blob [英] Blob from javascript binary string

查看:25
本文介绍了来自 javascript 二进制字符串的 Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 FileReader.readAsBinaryString(blob) 创建的二进制字符串.

我想用这个二进制字符串中表示的二进制数据创建一个 Blob.

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

推荐答案

您使用的 blob 是否不再可用?
你必须使用 readAsBinaryString 吗?您可以改用 readAsArrayBuffer 吗?使用数组缓冲区,重新创建 blob 会容易得多.

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 来重建 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">

这篇关于来自 javascript 二进制字符串的 Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆