Socket
Socket
Sign inDemoInstall

file-selector

Package Overview
Dependencies
1
Maintainers
3
Versions
30
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    file-selector

Convert DataTransfer object to a list of File objects


Version published
Weekly downloads
2.4M
decreased by-15.24%
Maintainers
3
Install size
233 kB
Created
Weekly downloads
 

Package description

What is file-selector?

The file-selector npm package is a utility library for handling file selections in web applications. It provides functions to convert file selections into a standardized format for easier manipulation, including functions to handle selections from both file inputs and drag-and-drop operations.

What are file-selector's main functionalities?

Converting FileList to Array

This feature allows you to convert a FileList object, which is obtained from a file input element, into an array of files for easier iteration and manipulation.

import { fromFileList } from 'file-selector';

const inputElement = document.querySelector('input[type="file"]');
inputElement.addEventListener('change', async (event) => {
  const filesArray = await fromFileList(event.target.files);
  // Now you have an array of files
});

Handling Drag-and-Drop Files

This feature provides a way to handle files dropped into a dropzone on a webpage, converting the DataTransferItem list from the event into an array of files.

import { fromEvent } from 'file-selector';

const dropzone = document.getElementById('dropzone');
dropzone.addEventListener('drop', async (event) => {
  event.preventDefault();
  const items = await fromEvent(event);
  // Now you have an array of files from the drop event
});

Other packages similar to file-selector

Readme

Source

file-selector

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

npm Tests codecov Open Collective Backers Open Collective Sponsors Contributor Covenant

Table of Contents

Installation

You can install this package from NPM:

npm add file-selector

Or with Yarn:

yarn add file-selector

CDN

For CDN, you can use unpkg:

https://unpkg.com/file-selector/dist/bundles/file-selector.umd.min.js

The global namespace for file-selector is fileSelector:

const {fromEvent} = fileSelector;
document.addEventListener('drop', async evt => {
    const files = await fromEvent(evt);
    console.log(files);
});

Usage

ES6

Convert a DragEvent to File objects:

import {fromEvent} from 'file-selector';
document.addEventListener('drop', async evt => {
    const files = await fromEvent(evt);
    console.log(files);
});

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

import {fromEvent} from 'file-selector';
const input = document.getElementById('myInput');
input.addEventListener('change', async evt => {
    const files = await fromEvent(evt);
    console.log(files);
});

Convert FileSystemFileHandle items to File objects:

import {fromEvent} from 'file-selector';

// Open file picker
const handles = await window.showOpenFilePicker({multiple: true});
// Get the files
const files = await fromEvent(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', async evt => {
    const files = await fromEvent(evt);
    console.log(files);
});

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

If you wish to contribute, please use the following guidelines:

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

Keywords

FAQs

Last updated on 30 Apr 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc