Skip to content Skip to sidebar Skip to footer

Error After Updating To Angular 7. Argument Of Type 'string | Arraybuffer' Is Not Assignable To Parameter Of Type 'string'

I upgraded my project from angular 6 to angular 7. I have a file upload component in my project. It gives a compiler error after the upgrade. onUpload() { const fileReader = ne

Solution 1:

Whilst result has the potential to return a string, it cannot implicitly cast this to a string as there is a risk of data loss. i.e. ArrayBuffer as string may result in data truncation (would have to test). So you have to explicitly cast it as to tell the compiler "I know what I am doing".

2 approaches to achieve this are:

(string)fileReader.result;
fileReader.result asstring;


Folks, Check @Malvolio's answer, it's more complete.

Solution 2:

You need a typeguard, like this:

if (this.fileToUploadinstanceofArrayBuffer) {
  // throw an error, 'cause you can't handle this
} else {
    fileReader.readAsText(this.fileToUpload);
}

Solution 3:

You could use the method toString() to parse the result:

const file = event.target.files[0];
 reader.readAsDataURL(file);

 const base64 = reader.result.toString().split(',')[1];

Solution 4:

Try

fileReader.onload = () => this.uploadFile(<string>fileReader.result);

Post a Comment for "Error After Updating To Angular 7. Argument Of Type 'string | Arraybuffer' Is Not Assignable To Parameter Of Type 'string'"