不能将类型'string'分配给类型'ArrayBuffer |ArrayLike< number>|SharedArrayBuffer'
我在 const arr = new Uint8Array(fileReader.result).subarray(0,4);
中出现错误[ts],尤其是在 fileReader.result
中第11行.
I get an error [ts] in const arr = new Uint8Array(fileReader.result).subarray(0, 4);
and particularly in fileReader.result
in line 11.
'string |类型的参数不能将ArrayBuffer分配给类型为ArrayBuffer |的参数.ArrayLike |SharedArrayBuffer'.
不能将类型'string'分配给类型'ArrayBuffer |ArrayLike |SharedArrayBuffer".
Argument of type 'string | ArrayBuffer' is not assignable to parameter of type 'ArrayBuffer | ArrayLike | SharedArrayBuffer'.
Type 'string' is not assignable to type 'ArrayBuffer | ArrayLike | SharedArrayBuffer'.
import { AbstractControl } from '@angular/forms';
import { Observable, Observer } from 'rxjs';
export const mimeType = (
control: AbstractControl
): Promise<{ [key: string]: any }> | Observable<{ [key: string]: any }> => {
const file = control.value as File;
const fileReader = new FileReader();
const frObs = Observable.create(
(observer: Observer<{ [key: string]: any }>) => {
fileReader.addEventListener('loadend', () => {
const arr = new Uint8Array(fileReader.result).subarray(0, 4);
let header = '';
let isValid = false;
for (let i = 0; i < arr.length; i++) {
header += arr[i].toString(16);
}
你们能分享一个解决方案吗?谢谢
Can you guys please share a solution. Thanks
您可以使用适当的类型转换来摆脱错误:例如
You can get rid of the error with appropriate type castings: e.g.
...而不是...
...instead of...
this.imagePreview = reader.result;
...尝试...
this.imagePreview = <string>.reader.result;
...或...
this.imagePreview = reader.result as string;
...或者代替...
...or instead of...
const arr = new Uint8Array(fileReader.result).subarray(0, 4);
...写...
const arr = new Uint8Array(<ArrayBuffer>fileReader.result).subarray(0, 4);
...或...
const arr = new Uint8Array(fileReader.result as ArrayBuffer).subarray(0, 4);
我希望这会有所帮助.