FileReader:readAsBinaryString() 方法

备注: 此特性在 Web Worker 中可用。

已弃用: 不再推荐使用该特性。虽然一些浏览器仍然支持它,但也许已从相关的 web 标准中移除,也许正准备移除或出于兼容性而保留。请尽量不要使用该特性,并更新现有的代码;参见本页面底部的兼容性表格以指导你作出决定。请注意,该特性随时可能无法正常工作。

备注: 此方法已弃用,取而代之的是 readAsArrayBuffer()

FileReader 接口的 readAsBinaryString() 方法用于开始读取指定 BlobFile 对象的内容。当读操作完成后,readyState 属性变为 DONE,并且 loadend 事件被触发。此时,result 属性包含来自的原始二进制数据文件。

请注意,此方法曾经从文件 API 规范中删除,但重新引入以实现向后兼容性。建议使用 FileReader.readAsArrayBuffer()

语法

js
readAsBinaryString(blob)

参数

blob

从中读取的 BlobFile 对象。

返回值

无(undefined)。

示例

js
const canvas = document.createElement("canvas");
const height = 200;
const width = 200;

canvas.width = width;
canvas.height = height;

const ctx = canvas.getContext("2d");

ctx.strokeStyle = "#090";
ctx.beginPath();
ctx.arc(width / 2, height / 2, width / 2 - width / 10, 0, Math.PI * 2);
ctx.stroke();

canvas.toBlob((blob) => {
  const reader = new FileReader();

  reader.onload = () => {
    console.log(reader.result);
  };

  reader.readAsBinaryString(blob);
});

规范

Specification
File API
# readAsBinaryString

浏览器兼容性

BCD tables only load in the browser

参见