import PromisifyFile from 'promisify-file'
;(async () => {
const testFile = new File(
['hello from promisify-file.txt'],
'promisify-file.txt',
{
type: 'text/plain',
lastModified: Date.now(),
},
)
const promisifyFile = new PromisifyFile(file)
try {
console.log(await promisifyFile.text()) // print `hello from promisify-file.txt`
} catch (err) {
console.error(err)
}
})()
{
name: fileName,
type: fileMimeType,
encoding: textFileEncoding,
quality: imageFileQuality,
lastModified: fileLastModified
}
syntax:
let promisifyFile = new PromisifyFile(data, options)
data must be a Blob
(include File
), for more data type support see PromisifyFile.from
returns a Promise
that resolves with a PromisifyFile
Object
syntax:
let promisifyFile = await PromisifyFile.from(data, options)
supported data types:
- Promise
- Blob
- XMLHttpRequest
- Body
- Document
- HTMLCanvasElement
- OffscreenCanvas
- HTMLImageElement
- RenderingContext
- ImageBitmapRenderingContext
- TypedArray
- DataView
- FileReader
- ImageData
- ImageBitmap
- DataURL (data:mimeType;Encoding,... or blob:...)
returns a Promise
that resolves with the result FileReader.readAs{ArrayBuffer, Text, DataURL, BinaryString}
syntax:
promisifyFile.arrayBuffer()
// optional `encoding` use in `FileReader.readAsText`
promisifyFile.text([encoding])
promisifyFile.dataURL()
promisifyFile.binaryString()
Notice: result of arrayBuffer / text / dataURL / binaryString is cached
PromisifyFile.prototype.{int8Array, uint8Array, uint8ClampedArray, int16Array, uint16Array, int32Array, uint32Array, float32Array, float64Array}
returns a Promise
that resolves with a TypedArray
created with blob arrayBuffer
optional arguments byteOffset
and length
same as TypedArray
syntax
syntax:
promisifyFile.int8Array([byteOffset [, length]])
promisifyFile.uint8Array([byteOffset [, length]])
promisifyFile.uint8ClampedArray([byteOffset [, length]])
promisifyFile.int16Array([byteOffset [, length]])
promisifyFile.uint16Array([byteOffset [, length]])
promisifyFile.uint32Array([byteOffset [, length]])
promisifyFile.float32Array([byteOffset [, length]])
promisifyFile.float64Array([byteOffset [, length]])
links:
returns a Promise
that resolves with a DataView
created with blob arrayBuffer
optional arguments byteOffset
and length
same as DataView
syntax
syntax:
promisifyFile.dataView([byteOffset [, length]])
links:
returns a Promise
that resolves with a new Blob
created with orignal blob
optional argument options
same as Blob
syntax
syntax:
promisifyFile.blob([options]])
links:
returns a Promise
that resolves with a new File
created with orignal blob
optional arguments name
and options
same as File
syntax
syntax:
promisifyFile.file([name [, options]])
Notice: if name is omitted, and the orignal Blob
don't has a name. a TypeError
throws
links:
returns a Promise
that resolves with the result of parsing blob text as JSON
syntax:
promisifyFile.json([encoding [, reviver]])
links:
returns a Promise
that resolves with the result of URL.createObjectURL
create with blob
syntax:
promisifyFile.url()
links:
returns a Promise
that resolves with loaded HTMLImageElement
create with blob
syntax:
promisifyFile.image()
links:
returns a Promise
that resolves with ImageBitmap
that createImageBitmap
returns
syntax:
promisifyFile.imageBitmap([options])
promisifyFile.imageBitmap(sx, sy, sw, sh[, options])
links:
returns a Promise
that resolves with ImageData
containing the image data of orignal image blob
syntax:
promisifyFile.imageData([sx = 0[, sy = 0[, sw = image.width [, sh = image.height]]]])
links:
- ImageData
- OffscreenCanvas
- HTMLCanvasElement
- CanvasRenderingContext2D
- CanvasRenderingContext2D.getImageData
returns a Promise
that resolves with DOMParser.parseFromString
parsing blob text
syntax:
promisifyFile.document([encoding [, overrideMimeType]])
promisifyFile.xml([encoding]) // shortcut for promisifyFile.document(encoding, 'application/xml')
promisifyFile.svg([encoding]) // shortcut for promisifyFile.document(encoding, 'image/svg+xml')
promisifyFile.html([encoding]) // shortcut for promisifyFile.document(encoding, 'text/html')
Notice:
Note that if the parsing process fails, the DOMParser
does not throw an exception, but PromisifyFile.document
throws an Error
links: