获取BLOB的二进制内容 [英] Getting the binary content of a BLOB

查看:83
本文介绍了获取BLOB的二进制内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,为了将BLOB对象转换为Java语言中的可读格式(URL),我应该使用createObjectURL()方法,对吗?

I know that, in order to convert a BLOB object to a readable format (URL) in Javascript, I should use the createObjectURL() method, right ?

示例:

var blob = new Blob(["Example"], { type: "text/plain" });
url = window.URL.createObjectURL(blob);

我的问题是:

是否可以获得BLOB的原始二进制文件内容?所以,我可以得到类似的东西:

Is it possible to get the raw binary content of a BLOB ? so, I can get something like :

"01000101 01111000 01100001 01101101 01110000 01101100 01100101" // "Example" in binary .

推荐答案

您可以使用"rel =" nofollow noreferrer"> FileReader .要获取数字ArrayBuffer,请使用 FileReader.readAsArrayBuffer() .

You can extract the data from the blob using a FileReader. To get an ArrayBuffer of numbers use FileReader.readAsArrayBuffer().

要将数组缓冲区转换为0和1的字符串,请创建 Int8array ),将其散布到一个数组中,然后

To convert the array buffer to a string of 0s and 1s, create an ArrayBufferView (Int8array in this case), spread it into an array, and then map the view to the binary representation of each number using Number.toString() with a radix of 2 - .toString(2):

const blob = new Blob(["Example"], { type: "text/plain" });

const reader = new FileReader();

reader.addEventListener("loadend", function() {  
  const view = new Int8Array(reader.result);
  
  const bin = [...view].map((n) => n.toString(2)).join(' ');
  
  console.log(bin);
});

reader.readAsArrayBuffer(blob);

这篇关于获取BLOB的二进制内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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