“文件”类型上不存在属性“arrayBuffer”

Mud*_*jaz 5 typescript angular

我在 Angular 应用程序中实现文件上传代码时遇到类型错误。我正在使用 Angular 8 和 TypeScript 3.5.3。以下是我的代码

模板 <input type="file" id="file" (change)="handleFileInput($event.target.files)">

以及组件类中的函数

  handleFileInput(files: FileList): void {
    files[0].arrayBuffer().then((value) => {
      console.log(value);
    });
   }
Run Code Online (Sandbox Code Playgroud)

编译时我收到此错误: error TS2339: Property 'arrayBuffer' does not exist on type 'File'.

知道如何消除这个错误吗?

Nik*_*las -1

将您的代码 html 更改为

<input type="file" id="file" (change)="handleFileInput($event)">
Run Code Online (Sandbox Code Playgroud)

你的 TS

handleFileInput(event: Event) {
   const file = (event.target as HTMLInputElement).files[0];
   const reader = new FileReader();
   let preview;
   reader.onload = () => {
     this.preview = reader.result as string;
   };
   reader.readAsDataURL(file);
}
Run Code Online (Sandbox Code Playgroud)