This is a simple wrapper for the FileReader
API. It allows converting a Blob
(and therefore a File
as well) to either a plain text string, a data URL or an ArrayBuffer
with a nice and clean Promise API.
This is the TypeScript signature of the readBlob
function:
/**
* Read a blob or file and convert it to another data type
*
* @param blob The blob or file to read
* @param target The format to target (defaults to "text")
*/
declare function readBlob(blob: Blob): Promise<string>
declare function readBlob(blob: Blob, target: 'text'): Promise<string>
declare function readBlob(blob: Blob, target: 'dataURL'): Promise<string>
declare function readBlob(blob: Blob, target: 'ArrayBuffer'): Promise<ArrayBuffer>
Usage is pretty difficult to visibly demonstrate without a surrounding DOM, so I made a demo on CodePen.