| 1 |
# file-selector |
| 2 |
|
| 3 |
> A small package for converting a [DragEvent](https://developer.mozilla.org/en-US/docs/Web/API/DragEvent) or [file input](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file) to a list of File objects. |
| 4 |
|
| 5 |
[](https://www.npmjs.com/package/file-selector) |
| 6 |
 |
| 7 |
[](https://coveralls.io/github/react-dropzone/file-selector?branch=master) |
| 8 |
[](#backers) |
| 9 |
[](#sponsors) |
| 10 |
[](https://github.com/react-dropzone/.github/blob/main/CODE_OF_CONDUCT.md) |
| 11 |
|
| 12 |
# Table of Contents |
| 13 |
|
| 14 |
* [Installation](#installation) |
| 15 |
* [Usage](#usage) |
| 16 |
* [Browser Support](#browser-support) |
| 17 |
* [Contribute](#contribute) |
| 18 |
* [Credits](#credits) |
| 19 |
* [Support](#support) |
| 20 |
* [License](#license) |
| 21 |
|
| 22 |
|
| 23 |
## Installation |
| 24 |
You can install this package from [NPM](https://www.npmjs.com): |
| 25 |
```bash |
| 26 |
npm add file-selector |
| 27 |
``` |
| 28 |
|
| 29 |
### CDN |
| 30 |
For CDN, you can use [unpkg](https://unpkg.com): |
| 31 |
|
| 32 |
[https://unpkg.com/file-selector/dist/bundles/file-selector.umd.min.js](https://unpkg.com/file-selector/dist/bundles/file-selector.umd.min.js) |
| 33 |
|
| 34 |
The global namespace for file-selector is `fileSelector`: |
| 35 |
```js |
| 36 |
const {fromEvent} = fileSelector; |
| 37 |
document.addEventListener('drop', async evt => { |
| 38 |
const files = await fromEvent(evt); |
| 39 |
console.log(files); |
| 40 |
}); |
| 41 |
``` |
| 42 |
|
| 43 |
|
| 44 |
## Usage |
| 45 |
|
| 46 |
### ES6 |
| 47 |
Convert a [DragEvent](https://developer.mozilla.org/en-US/docs/Web/API/DragEvent) to File objects: |
| 48 |
```ts |
| 49 |
import {fromEvent} from 'file-selector'; |
| 50 |
document.addEventListener('drop', async evt => { |
| 51 |
const files = await fromEvent(evt); |
| 52 |
console.log(files); |
| 53 |
}); |
| 54 |
``` |
| 55 |
|
| 56 |
Convert a [change event](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event) for an input type file to File objects: |
| 57 |
```ts |
| 58 |
import {fromEvent} from 'file-selector'; |
| 59 |
const input = document.getElementById('myInput'); |
| 60 |
input.addEventListener('change', async evt => { |
| 61 |
const files = await fromEvent(evt); |
| 62 |
console.log(files); |
| 63 |
}); |
| 64 |
``` |
| 65 |
|
| 66 |
Convert [FileSystemFileHandle](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle) items to File objects: |
| 67 |
```ts |
| 68 |
import {fromEvent} from 'file-selector'; |
| 69 |
|
| 70 |
// Open file picker |
| 71 |
const handles = await window.showOpenFilePicker({multiple: true}); |
| 72 |
// Get the files |
| 73 |
const files = await fromEvent(handles); |
| 74 |
console.log(files); |
| 75 |
``` |
| 76 |
**NOTE** The above is experimental and subject to change. |
| 77 |
|
| 78 |
### CommonJS |
| 79 |
Convert a `DragEvent` to File objects: |
| 80 |
```ts |
| 81 |
const {fromEvent} = require('file-selector'); |
| 82 |
document.addEventListener('drop', async evt => { |
| 83 |
const files = await fromEvent(evt); |
| 84 |
console.log(files); |
| 85 |
}); |
| 86 |
``` |
| 87 |
|
| 88 |
|
| 89 |
## Browser Support |
| 90 |
Most browser support basic File selection with drag 'n' drop or file input: |
| 91 |
* [File API](https://developer.mozilla.org/en-US/docs/Web/API/File#Browser_compatibility) |
| 92 |
* [Drag Event](https://developer.mozilla.org/en-US/docs/Web/API/DragEvent#Browser_compatibility) |
| 93 |
* [DataTransfer](https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer#Browser_compatibility) |
| 94 |
* [`<input type="file">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Browser_compatibility) |
| 95 |
|
| 96 |
For folder drop we use the [FileSystem API](https://developer.mozilla.org/en-US/docs/Web/API/FileSystem) which has very limited support: |
| 97 |
* [DataTransferItem.getAsFile()](https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/getAsFile#Browser_compatibility) |
| 98 |
* [DataTransferItem.webkitGetAsEntry()](https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/webkitGetAsEntry#Browser_compatibility) |
| 99 |
* [FileSystemEntry](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemEntry#Browser_compatibility) |
| 100 |
* [FileSystemFileEntry.file()](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileEntry/file#Browser_compatibility) |
| 101 |
* [FileSystemDirectoryEntry.createReader()](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryEntry/createReader#Browser_compatibility) |
| 102 |
* [FileSystemDirectoryReader.readEntries()](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryReader/readEntries#Browser_compatibility) |
| 103 |
|
| 104 |
|
| 105 |
## Contribute |
| 106 |
Checkout the organization [CONTRIBUTING.md](https://github.com/react-dropzone/.github/blob/main/CONTRIBUTING.md). |
| 107 |
|
| 108 |
## Credits |
| 109 |
* [html5-file-selector](https://github.com/quarklemotion/html5-file-selector) |
| 110 |
|
| 111 |
## Support |
| 112 |
|
| 113 |
### Backers |
| 114 |
Support us with a monthly donation and help us continue our activities. [[Become a backer](https://opencollective.com/react-dropzone#backer)] |
| 115 |
|
| 116 |
<a href="https://opencollective.com/react-dropzone/backer/0/website" target="_blank"><img src="https://opencollective.com/react-dropzone/backer/0/avatar.svg"></a> |
| 117 |
<a href="https://opencollective.com/react-dropzone/backer/1/website" target="_blank"><img src="https://opencollective.com/react-dropzone/backer/1/avatar.svg"></a> |
| 118 |
<a href="https://opencollective.com/react-dropzone/backer/2/website" target="_blank"><img src="https://opencollective.com/react-dropzone/backer/2/avatar.svg"></a> |
| 119 |
<a href="https://opencollective.com/react-dropzone/backer/3/website" target="_blank"><img src="https://opencollective.com/react-dropzone/backer/3/avatar.svg"></a> |
| 120 |
<a href="https://opencollective.com/react-dropzone/backer/4/website" target="_blank"><img src="https://opencollective.com/react-dropzone/backer/4/avatar.svg"></a> |
| 121 |
<a href="https://opencollective.com/react-dropzone/backer/5/website" target="_blank"><img src="https://opencollective.com/react-dropzone/backer/5/avatar.svg"></a> |
| 122 |
<a href="https://opencollective.com/react-dropzone/backer/6/website" target="_blank"><img src="https://opencollective.com/react-dropzone/backer/6/avatar.svg"></a> |
| 123 |
<a href="https://opencollective.com/react-dropzone/backer/7/website" target="_blank"><img src="https://opencollective.com/react-dropzone/backer/7/avatar.svg"></a> |
| 124 |
<a href="https://opencollective.com/react-dropzone/backer/8/website" target="_blank"><img src="https://opencollective.com/react-dropzone/backer/8/avatar.svg"></a> |
| 125 |
<a href="https://opencollective.com/react-dropzone/backer/9/website" target="_blank"><img src="https://opencollective.com/react-dropzone/backer/9/avatar.svg"></a> |
| 126 |
<a href="https://opencollective.com/react-dropzone/backer/10/website" target="_blank"><img src="https://opencollective.com/react-dropzone/backer/10/avatar.svg"></a> |
| 127 |
|
| 128 |
### Sponsors |
| 129 |
Become a sponsor and get your logo on our README on Github with a link to your site. [[Become a sponsor](https://opencollective.com/react-dropzone#sponsor)] |
| 130 |
|
| 131 |
<a href="https://opencollective.com/react-dropzone/sponsor/0/website" target="_blank"><img src="https://opencollective.com/react-dropzone/sponsor/0/avatar.svg"></a> |
| 132 |
<a href="https://opencollective.com/react-dropzone/sponsor/1/website" target="_blank"><img src="https://opencollective.com/react-dropzone/sponsor/1/avatar.svg"></a> |
| 133 |
<a href="https://opencollective.com/react-dropzone/sponsor/2/website" target="_blank"><img src="https://opencollective.com/react-dropzone/sponsor/2/avatar.svg"></a> |
| 134 |
<a href="https://opencollective.com/react-dropzone/sponsor/3/website" target="_blank"><img src="https://opencollective.com/react-dropzone/sponsor/3/avatar.svg"></a> |
| 135 |
<a href="https://opencollective.com/react-dropzone/sponsor/4/website" target="_blank"><img src="https://opencollective.com/react-dropzone/sponsor/4/avatar.svg"></a> |
| 136 |
<a href="https://opencollective.com/react-dropzone/sponsor/5/website" target="_blank"><img src="https://opencollective.com/react-dropzone/sponsor/5/avatar.svg"></a> |
| 137 |
<a href="https://opencollective.com/react-dropzone/sponsor/6/website" target="_blank"><img src="https://opencollective.com/react-dropzone/sponsor/6/avatar.svg"></a> |
| 138 |
<a href="https://opencollective.com/react-dropzone/sponsor/7/website" target="_blank"><img src="https://opencollective.com/react-dropzone/sponsor/7/avatar.svg"></a> |
| 139 |
<a href="https://opencollective.com/react-dropzone/sponsor/8/website" target="_blank"><img src="https://opencollective.com/react-dropzone/sponsor/8/avatar.svg"></a> |
| 140 |
<a href="https://opencollective.com/react-dropzone/sponsor/9/website" target="_blank"><img src="https://opencollective.com/react-dropzone/sponsor/9/avatar.svg"></a> |
| 141 |
<a href="https://opencollective.com/react-dropzone/sponsor/10/website" target="_blank"><img src="https://opencollective.com/react-dropzone/sponsor/10/avatar.svg"></a> |
| 142 |
|
| 143 |
## License |
| 144 |
MIT |