此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

FileReader:readAsBinaryString() 方法

Deprecated

Avoid using this feature in new projects. This feature may be a candidate for removal from web standards or browsers.

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

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

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

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

语法

js
readAsBinaryString(blob)

参数

blob

从中读取的 Blob 或 File 对象。

返回值

无(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);
});

规范

规范
File API
# readAsBinaryString

浏览器兼容性

参见