0

I'm trying to convert a File into a Blob without using FileReader. I can't use FileReader because the latest version of Typescript (which I must use) doesn't accept a File as the parameter to fileReader.readAsArrayBuffer().

Using FileReader I can do it as follows:

var reader = new FileReader();

reader.onloadend = function(e) {
  new Blob([ this.result ], { type: "image/jpeg" } );
};

reader.readAsArrayBuffer(file);

How can I achieve the same result without FileReader?

1
  • If I remember right, File is a subclass of Blob, so you shouldn't even need to use FileReader, it just already is a Blob. You could always cast to an any though if Typescript has issues with it. Commented May 2, 2017 at 17:12

1 Answer 1

1

You can always augment the declaration in lib.d.ts with an overload accepting a File.

For example, create a file named globals.d.ts with the following content

interface FileReader {
    readAsArrayBuffer(file: File): void;
}

That said, if the declaration was changed, I would be wary of this behavior as some environments likely do not support it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.