https://github.com/valesdev/file-promisify
https://github.com/valesdev/file-promisify
file files image promise
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/valesdev/file-promisify
- Owner: valesdev
- License: mit
- Created: 2020-06-20T10:11:48.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2025-10-30T08:49:17.000Z (8 months ago)
- Last Synced: 2025-10-30T13:42:44.129Z (8 months ago)
- Topics: file, files, image, promise
- Language: TypeScript
- Homepage:
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# file-promisify
[](https://www.npmjs.com/package/file-promisify)
[](https://npmcharts.com/compare/file-promisify?minimal=true)
[](https://www.npmjs.com/package/file-promisify)
Utilities for file and image handling, in Browsers, with Promise.
[Live demo](https://open.vales.io/file-promisify/demo.html)
## Installation
### Node.js
```sh
$ npm install --save file-promisify
```
### Browser via CDN
```html
```
## Usage
```js
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 => {})
```
## API
### `instance.select({ multiple: [multiple], accept: [accept] })`
Open a file dialog.
| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| `multiple` | Boolean | Multiple selection or not. | `false` |
| `accept` | String | MIME type accepted. | `'*/*'` |
- Returns: `Promise`
### `instance.selectDirectory()`
Open a directory dialog. (only webkit)
- Returns: `Promise`
### `Files.processImage({ blob: , width: [width], height: [height], crop: [crop] })`
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`
### `Files.urlToImage()`
Fetch image URL into Image instance.
| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| `url` | String | The URL of image. | (required) |
- Returns: `Promise`
### `Files.blobToDataUrl()`
Transform Blob to data URL.
| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| `blob` | Blob | The blob. | (required) |
- Returns: `Promise`
### `Files.dataUrlToBlob()`
Transform data URL to Blob.
| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| `dataUrl` | String | The data url. | (required) |
- Returns: `Promise`
### `Files.dataUrlToBase64()`
Transform data URL to Base64 encoded string.
| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| `dataUrl` | String | The data url. | (required) |
- Returns: `Promise`
### `Files.blobToBase64()`
Transform Blob to Base64 encoded string.
| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| `blob` | Blob | The blob. | (required) |
- Returns: `Promise`
### `Files.blobToArrayBuffer()`
Transform Blob to ArrayBuffer.
| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| `blob` | Blob | The blob. | (required) |
- Returns: `Promise`
### `Files.blobToString(, [encoding])`
Transform Blob to string.
| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| `blob` | Blob | The blob. | (required) |
| `encoding` | String | The encoding. | `'UTF-8'` |
- Returns: `Promise`
### `Files.stringToBlob(, [type])`
Transform string to Blob.
| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| `string` | String | The string. | (required) |
| `type` | String | The MIME type. | `'application/octet-stream'` |
- Returns: `Promise`
### `Files.stringToByteArray()`
Transform string to byte array.
| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| `string` | String | The string. | (required) |
- Returns: `Promise`
### `Files.getImageOrientation()`
Get image orientation value from Blob.
| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| `blob` | Blob | The blob of image data. | (required) |
- Returns: `Promise`
### `Files.getMimeTypeFromDataUrl()`
Get MIME type from data URL.
| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| `dataUrl` | String | The data url. | (required) |
- Returns: `Promise`
## License
[MIT](http://opensource.org/licenses/MIT)