https://github.com/tschoffelen/pick-a-file
JS helper to ask the user to pick a file.
https://github.com/tschoffelen/pick-a-file
Last synced: 2 months ago
JSON representation
JS helper to ask the user to pick a file.
- Host: GitHub
- URL: https://github.com/tschoffelen/pick-a-file
- Owner: tschoffelen
- License: mit
- Created: 2024-08-21T17:26:16.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-09-03T07:34:19.000Z (9 months ago)
- Last Synced: 2025-03-06T05:20:29.245Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 53.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Pick a file
**Ask the user to pick a file, get a promise with a [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File) back.**
Does this need to be a package? No, probably not, you might as well copy and paste the code from
[`index.js`](src/index.js) into your project. But I kept re-writing this little snippet, and I
thought I'd just make it easy for myself to access it.## Installation
Run `yarn add pick-a-file`, or `npm i pick-a-file` if you insist.
## Usage
```js
import pickFile from 'pick-a-file';const file = await pickFile({
accept: ".csv",
});if (file) {
console.log(`Nice, you selected a file: ${file.name}`);
} else {
console.log(`Boohoo, why did you not select a file?!`);
}
```Options:
- [`accept`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#accept): specify a list of comma-separated file extensions or MIME types to accept.
- [`multiple`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#multiple): whether to allow the user to select multiple items. When set to `true`, the promise will resolve with a [`FileList`](https://developer.mozilla.org/en-US/docs/Web/API/FileList) rather than a single `File`. Defaults to `false`.