如何将选中的文件转换为blob格式?

How to convert the selected file to blob format?

我有简单的输入文件字段。

<input  type="file"
       id="openselect"
       name="files[]"
       (change)="fileSelect($event)">

它有相关的功能,尝试将所选文件转换为 BLOB 格式。

openselect.addEventListener("change", fileSelect, false);
function fileSelect(event) {
  const file = event.target['files'][0];
  const fileReader = new FileReader();
  fileReader.readAsText(file);
  console.log(fileReader.result)    // null
  console.log('iamge as blob: ', new Blob([fileReader.result]))        // meta info
}

因此,控制台仅显示文件元信息,但不显示 BLOB 字符串。

请帮助我将所选文件转换为 BLOB 字符串。

这里是live example.

查看 Mozilla 的 FileReader 文档 (https://developer.mozilla.org/en-US/docs/Web/API/FileReader/result):

This example presents a function, read(), which reads a file from a file input. It works by creating a FileReader object and creating a listener for load events such that when then file is read, the result is obtained and passed to the callback function provided to read().

解决方案:Live Example

另一个可能对您有帮助的问题HTML5 FileReader how to return result?