Utilities for file and image handling, in Browsers, with Promise.
$ npm install --save file-promisify
<script src="https://cdn.jsdelivr.net/npm/file-promisify/dist/index.umd.js"></script>
import Files from 'file-promisify'
const instance = new Files()
/** open file dialog */
instance.select()
.then(([blob]) => {})
.catch(error => {})
/** open file dialog for selecting multiple files */
instance.select({ multiple: true })
.then(blobs => {})
.catch(error => {})
/** open file dialog for selecting an image file */
instance.select({ accept: 'image/*' })
.then(blob => {
/** wrap image into maximum 128 × 128 pixels */
Files.processImage({ blob, width: 128, height: 128, crop: false })
.then(dataUrl => {})
.catch(error => {})
/** crop image into 128 × 128 pixels exactly */
Files.processImage({ blob, width: 128, height: 128, crop: true })
.then(dataUrl => {})
.catch(error => {})
})
.catch(error => {})
/** open directory dialog for selecting a directory (only webkit) */
instance.selectDirectory()
.then(blobs => {})
.catch(error => {})
Open a file dialog.
Parameter | Type | Description | Default |
---|---|---|---|
multiple |
Boolean | Multiple selection or not. | false |
accept |
String | MIME type accepted. | '*/*' |
- Returns:
Promise<FileList>
Open a directory dialog. (only webkit)
- Returns:
Promise<FileList>
Process image.
Parameter | Type | Description | Default |
---|---|---|---|
blob |
Blob | The blob of image data. | (required) |
width |
Number | Target width. | null |
height |
Number | Target height. | null |
crop |
Boolean | Should crop or not. true for cropping image into dimension exactly, while false for wrapping image into the maximum dimension. |
false |
- Returns:
Promise<String>
Fetch image URL into Image instance.
Parameter | Type | Description | Default |
---|---|---|---|
url |
String | The URL of image. | (required) |
- Returns:
Promise<Image>
Transform Blob to data URL.
Parameter | Type | Description | Default |
---|---|---|---|
blob |
Blob | The blob. | (required) |
- Returns:
Promise<String>
Transform data URL to Blob.
Parameter | Type | Description | Default |
---|---|---|---|
dataUrl |
String | The data url. | (required) |
- Returns:
Promise<Blob>
Transform data URL to Base64 encoded string.
Parameter | Type | Description | Default |
---|---|---|---|
dataUrl |
String | The data url. | (required) |
- Returns:
Promise<String>
Transform Blob to Base64 encoded string.
Parameter | Type | Description | Default |
---|---|---|---|
blob |
Blob | The blob. | (required) |
- Returns:
Promise<String>
Transform Blob to ArrayBuffer.
Parameter | Type | Description | Default |
---|---|---|---|
blob |
Blob | The blob. | (required) |
- Returns:
Promise<ArrayBuffer>
Transform Blob to string.
Parameter | Type | Description | Default |
---|---|---|---|
blob |
Blob | The blob. | (required) |
encoding |
String | The encoding. | 'UTF-8' |
- Returns:
Promise<String>
Transform string to Blob.
Parameter | Type | Description | Default |
---|---|---|---|
string |
String | The string. | (required) |
type |
String | The MIME type. | 'application/octet-stream' |
- Returns:
Promise<Blob>
Transform string to byte array.
Parameter | Type | Description | Default |
---|---|---|---|
string |
String | The string. | (required) |
- Returns:
Promise<Uint8Array>
Get image orientation value from Blob.
Parameter | Type | Description | Default |
---|---|---|---|
blob |
Blob | The blob of image data. | (required) |
- Returns:
Promise<Number>
Get MIME type from data URL.
Parameter | Type | Description | Default |
---|---|---|---|
dataUrl |
String | The data url. | (required) |
- Returns:
Promise<String>