1

When I transfer a file object (File interface) using Socket.IO with Typescript, the file becomes an ArrayBuffer on the receiving end.

Is there any way to transform the ArrayBuffer back into a File, or should I not send the entire file object using socket io (send name, data, mime type separately)?

1
  • File is just a special Blob, ArrayBuffer to Blob is easy. Commented Aug 13, 2017 at 17:34

1 Answer 1

2

Pass the ArrayBuffer to Blob or File constructor

const file = new Blob([ab])

const file = new File([ab], "filename.ext", {type:/* valid MIME type */})
Sign up to request clarification or add additional context in comments.

3 Comments

I tried that, var file : File = new Blob(arraybuffer); however, I get the error that the Blob class is missing the lastModifiedDate that the File requires, and so cannot be converted. If I force it using typecasting, I get the error: core.es5.js:1084 ERROR TypeError: Failed to construct 'Blob': Iterator getter is not callable.
Missed the update, will try the second line, thanks!
If lastModifiedDate is required by your application you can set that property at the object at the third parameter of File() constructor

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.