Skip to content

Repository files navigation

file-selector logo

file-selector

A small package for converting a DragEvent or file input to a list of File objects.

📖 Documentation & live playground →

npmTestscoverageOpen Collective BackersOpen Collective SponsorsContributor Covenant

Table of Contents

Installation

You can install this package from NPM:

npm add file-selector

CDN

For CDN usage, load the ESM build directly from a CDN such as esm.sh or jsDelivr:

<scripttype="module">import{fromEvent}from"https://esm.sh/file-selector";document.addEventListener("drop",asyncevt=>{constfiles=awaitfromEvent(evt);console.log(files);});</script>

Usage

ES6

Convert a DragEvent to File objects:

import{fromEvent}from"file-selector";document.addEventListener("drop",asyncevt=>{constfiles=awaitfromEvent(evt);console.log(files);});

Convert a change event for an input type file to File objects:

import{fromEvent}from"file-selector";constinput=document.getElementById("myInput");input.addEventListener("change",asyncevt=>{constfiles=awaitfromEvent(evt);console.log(files);});

Convert FileSystemFileHandle items to File objects:

import{fromEvent}from"file-selector";// Open file pickerconsthandles=awaitwindow.showOpenFilePicker({multiple: true});// Get the filesconstfiles=awaitfromEvent(handles);console.log(files);

NOTE The above is experimental and subject to change.

CommonJS

Convert a DragEvent to File objects:

const{fromEvent}=require("file-selector");document.addEventListener("drop",asyncevt=>{constfiles=awaitfromEvent(evt);console.log(files);});

MIME types

When the browser doesn't set a File's type, fromEvent infers one from the file extension. By default it uses a small built-in table of the most common types, keeping the bundle lean.

If you need broader coverage, import the full extension-to-MIME table from the file-selector/mime subpath and pass it via the mimeTypes option. Because it's a separate entry point, the full table (~1,200 entries) is only included in your bundle when you import it:

import{fromEvent}from"file-selector";import{COMMON_MIME_TYPES}from"file-selector/mime";constfiles=awaitfromEvent(evt,{mimeTypes: COMMON_MIME_TYPES});

You can also pass your own Map<extension, mimeType> to restrict or extend the lookup:

constfiles=awaitfromEvent(evt,{mimeTypes: newMap([["dwg","image/vnd.dwg"]])});

Browser Support

Most browser support basic File selection with drag 'n' drop or file input:

For folder drop we use the FileSystem API which has very limited support:

Contribute

Checkout the organization CONTRIBUTING.md.

Development

Development requires Node.js >= 20. Install the dependencies with:

npm install

The project is built with tsdown (Rolldown), type-checked with TypeScript, tested with Vitest and linted/formatted with oxlint and oxfmt.

CommandDescription
npm testRun the test suite in watch mode.
npm run test:covRun the tests once with coverage (runs type-check, lint and format check first).
npm run type-checkType-check the sources without emitting.
npm run lintLint the sources (oxlint).
npm run lint:fixApply safe lint fixes.
npm run lint:type-awareType-aware lint via tsgolint (alpha).
npm run formatFormat the sources (oxfmt).
npm run format:checkCheck formatting without writing.
npm run buildBuild the outputs into dist/.
npm run devBuild in watch mode.
npm run docs:devRun the Vocs docs site locally.
npm run docs:buildBuild the docs site into site/.
npm run docs:previewPreview the built docs site.
npm run hooks:installInstall the git hooks (prek install).
npm run hooks:runRun every git hook over the whole repo.

The build emits into dist/:

  • dist/index.js — ES module
  • dist/index.cjs — CommonJS module
  • dist/index.d.ts — TypeScript declarations

Git hooks

Git hooks are managed with prek, a fast Rust drop-in for the pre-commit framework (configured in .pre-commit-config.yaml). On commit they format and lint the staged files with oxfmt/oxlint, and check the message against Conventional Commits — the same convention semantic-release uses to cut releases.

prek is a standalone binary, so install it once (e.g. uv tool install prek, brew install prek, or see the install docs). npm install then wires up the hooks automatically; run npm run hooks:install to (re)install them by hand. The hooks are optional — CI enforces the same lint, format and commit checks — but they catch issues before you push.

Credits

Support

Backers

Support us with a monthly donation and help us continue our activities. [Become a backer]

Sponsors

Become a sponsor and get your logo on our README on Github with a link to your site. [Become a sponsor]

License

MIT

About

Convert a DragEvent or file input to a list of File objects

Topics

Resources

Code of conduct

Contributing

Stars

104 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages