Blob from javascript binary string(来自 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:来自 javascript 二进制字符串的 Blob
基础教程推荐
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
