Search code examples
deno

How to load a binary file into Blob?


The Larger Context: I am attaching a file to a Confluence page. This is done by POSTing a multi-part request, containing the file, to the Confluence RESTful API.

I'm looking for a simple means of loading a complete binary file (a PNG) into a Blob, so that I can compose the FormData object. The file is small, (less than a Meg) so I am content to load it all into memory.

I can compose the Blob from byte literals, but cannot see yet how I can load file data into it.


Solution

  • The answer came to be shortly after.

        const fileBytes = await Deno.readFile(filename);
        const fileBlob = new Blob([fileBytes], {type: 'image/png'});