Git Product home page Git Product logo

file-dialog's Introduction

file-dialog

npm version PRs Welcome

Directly call the file browser dialog from your code, and get back the resulting array of FileList. Handy for when you need to post files via AJAX/Fetch. No more hacky hiding of <input type="file"> elements. Support for Callbacks & Promises!

  • Supports ES6 Modules, CommonJS, AMD, and global
  • Supports selecting multiple files and the file type 'accepts' attribute (see examples below)
  • Support for all major browsers
  • No jQuery needed, tiny (1.25 KB), with no dependencies

alt text

Install

Supports both CommonJS and ES6 Modules

  1. npm install file-dialog
  2. import fileDialog from 'file-dialog' or const fileDialog = require('file-dialog')

Note: If you want to support older browsers make sure you have babel enabled.

Classic <script> includes

  1. Include minified file-dialog.min.js via <script>
  2. Module is binded to the global variable fileDialog

Examples

Get a File via a promise and POST to server via Fetch

fileDialog()
    .then(file => {
        const data = new FormData()
        data.append('file', file[0])
        data.append('imageName', 'flower')

        // Post to server
        fetch('/uploadImage', {
            method: 'POST',
            body: data
        })
    })

Allow user to select only an image file

fileDialog({ accept: 'image/*' })
    .then(files => {
        // files contains an array of FileList
    })

Allow user to select only images or videos

    
fileDialog({ accept: ['image/*', 'video/*'] })
    .then(files => {
        // files contains an array of FileList
    })

Allow user to select multiple image files at once

fileDialog({ multiple: true, accept: 'image/*' })
    .then(files => {
        // files contains an array of FileList
    })

Classic callback version of the above

fileDialog({ multiple: true, accept: 'image/*' }, files => {
    // files contains an array of FileList
})

file-dialog's People

Contributors

alnorris avatar anwerso avatar errorx666 avatar jasonbarry avatar viviivanov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

file-dialog's Issues

Does not trigger always on Chrome

I am using Chrome Version 72.0.3626.109 (Official Build) (64-bit)
In my html, I have HTML dropdown and I want to trigger the fileDialog open event on selection change.

This works 8/10 times but not always. Firefox works everytime.

Is there any permanent solution for that?

main = file-dialog-min.js ?

Hi,
thanks for making this module available. Would it make sense to link main to the minified file? I'm using this with Meteor, which supports Babel and import statements, but which expects libraries to be already transformed into ES5, so trying to build an app which includes this module fails at the uglify stage (because the code doesn't get run through Babel, even though my own code is ES6, and does get run through babel).

I guess eventually there might be a way to specify an ES5 and an ES6 main, since I understand that people using rollup etc want access to the unminified code...

Open Directory

It would be nice to have support opening directory
<input type="file" webkitdirectory directory multiple />

Not working in IE and Edge

Change event fired immediately after input.dispatchEvent(evt) in IE, so to fix it we need to move input.dispatchEvent(evt) after attaching "change" listener in Promise.

IE support

Hi! The README says "Support for all major browsers", could you please be more specific in the description at least with the minimum supported versions? I guess IE11 is supported, and maybe IE10, or am I wrong here? Thank you!

Made some modifications

Hi,

Thanks for this.

I made some changes for my TypeScript based application as the provided TS definitions was not good for me.

The only minor issue I have is the promise will never resolve or reject if user clicks cancel when the file input dialog is opened - potential memory leak if done repeatedly.

Sharing here FWIW:

// modified based on alnorris/file-dialog
// https://github.com/alnorris/file-dialog/blob/master/file-dialog.d.ts
// - removed support for callback
// - improved TypeScript type definitions
// - directly click() on input
// - do not add input to DOM (seems to work in Chrome - no plans to support MSIE)
export const fileDialog = (options?: { multiple?: boolean, accept?: string }) => {
  const input = document.createElement('input')

  // Set config
  if (options) {
    if (options.multiple) {
      input.setAttribute('multiple', '')
    }
    if (options.accept !== undefined) {
      input.setAttribute('accept', options.accept)
    }
  }

  input.setAttribute('type', 'file')

  // Return promise
  return new Promise<FileList | null>(resolve => {
    input.addEventListener('change', () => resolve(input.files))
    input.click()
  })
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.