npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-simple-file-input

v2.1.0

Published

Simple wrapper for the HTML input tag and HTML5 FileReader API

Downloads

8,275

Readme

react-simple-file-input

npm GitHub license

Simple wrapper for the HTML input tag and HTML5 FileReader API that supports multiple files

Usage

var FileInput = require('react-simple-file-input');

<FileInput
  readAs='binary'
  multiple

  onLoadStart={ this.showProgressBar }
  onLoad={ this.handleFileSelected }
  onProgress={ this.updateProgressBar }

  cancelIf={ checkIfFileIsIncorrectFiletype }
  abortIf={ this.cancelButtonClicked }

  onCancel={ this.showInvalidFileTypeMessage }
  onAbort={ this.resetCancelButtonClicked }
 />

Installation

React >=0.14.9

npm install react-simple-file-input --save

React <0.14.9

npm install [email protected] --save

Options

readAs

react-simple-file-input expects a readAs option to indicate how the file should be read. Valid options are:

  • 'text' (Text)
  • 'buffer' (Array Buffer)
  • 'binary' (Binary String)
  • 'dataUrl' (Data URL)

By default the readAs option is undefined. If left undefined, the file will not be read from disk and only the onChange event will be triggered.

Events

react-simple-file-input supports the following event handlers:

FileReader events

  • onLoadStart
  • onProgress
  • onLoadEnd
  • onLoad
  • onAbort
  • onError

Each event handler receives the native event as the first argument and the selected File object as the second.

Custom events

  • onChange
  • onCancel

The onChange handler is called whenever the file is changed and occurs before the file is read from disk. It receives a File object as its only argument when the multiple prop is false (or left undefined), otherwise it receives an array-like list of File objects as its only argument.

The onCancel handler receives the File object corresponding to the file the user attempted to read from the file system.

Allowing multiple files to be selected

By setting the multiple prop to a truthy value, you allow the user to select multiple files at once, using the same input by either ctrl + click or shift + clicking more than one file in file selection modal that appears.

This causes the onChange handler to receive a list of File objects (even if it's a only one file) rather a single File object. All other handlers are triggered independently, for each file the user has selected, so cancelling, aborting and monitoring the progress of reading each file may be done separately.

If you wish to cancel or abort all file reads if one fails, this must be done by maintaining state externally, in your handlers.

Skipping file reads

If the readAs option is not specified, the file will not be read from disk and only onChange will be triggered. All other events as skipped.

Aborting and cancelling file reads

There are two props for canceling and aborting file reads:

Cancelling the file read before it begins

The cancelIf prop accepts a function that receives the File object. If the function is defined and returns a truthy value then the file upload will be cancelled before it begins reading from the filesystem and the onCancel handler is called with the File object.

Aborting the file read once it has begun

If defined, the abortIf function is executed just before every time the onProgress handler is called. It is passed the native event and the File object the first and second arguments; if it returns a truthy value, the file read is aborted and the onAbort handler is called. The onProgress event is not called if the file read aborts.

Children

Children are not supported. If you wish to use another element to set the clickable area for the user, use labels or a similar strategy (see example below).

Styling, hiding or replacing the default input field

All props passed to FileInput are passed to the resulting <input> tag so it's possible to style or hide the default input field by passing style or className prop values.

To replace the input field with another element, hide it and use a parent label as demonstrated in the example below:

Examples

import React, { Component } from 'react';
var FileInput = require('react-simple-file-input');

var allowedFileTypes = ["image/png", "image/jpeg", "image/gif"];

function fileIsIncorrectFiletype(file){
  if (allowedFileTypes.indexOf(file.type) === -1) {
    return true;
  } else {
    return false;
  }
}

class AssetsForm extends Component {
  constructor(props, context) {
    super(props, context);

    this.cancelButtonClicked = this.cancelButtonClicked.bind(this);
    this.resetCancelButtonClicked = this.resetCancelButtonClicked.bind(this);
    this.showProgressBar = this.showProgressBar.bind(this);
    this.updateProgressBar = this.updateProgressBar.bind(this);
    this.handleFileSelected = this.handleFileSelected.bind(this);

    this.state = {
      cancelButtonClicked: false
    };
  }

  render(){
    return(
      <div>
        To upload a file:

         <label >
            <FileInput
              readAs='binary'
              style={ { display: 'none' } }

              onLoadStart={this.showProgressBar}
              onLoad={this.handleFileSelected}
              onProgress={this.updateProgressBar}

              cancelIf={fileIsIncorrectFiletype}
              abortIf={this.cancelButtonClicked}

              onCancel={this.showInvalidFileTypeMessage}
              onAbort={this.resetCancelButtonClicked}
             />

           <span >
            Click Here
           </span>

         </label>
      </div>
    );
  }

  cancelButtonClicked(){
    return this.state.cancelButtonClicked;
  }

  resetCancelButtonClicked(){
    this.setState({ cancelButtonClicked: false });
  }

  showInvalidFileTypeMessage(file){
    window.alert("Tried to upload invalid filetype " + file.type);
  }

  showProgressBar(){
    this.setState({ progressBarVisible: true});
  }

  updateProgressBar(event){
    this.setState({
      progressPercent: (event.loaded / event.total) * 100
    });
  }

  handleFileSelected(event, file){
    this.setState({file: file, fileContents: event.target.result});
  }
}

Upgrading from 0.x.x

To upgrade from 0.x.x to 1.0.0, you simply need to move any children of FileInput into an enclosing label tag, as shown in the example above and pass some styling using the style or className props to hide the default input field.

Contributors

Thank you to github users selbekk and Zhouzi for your valued contributions via pull requests. They ended up informing most of the improvements in version 1.0.0.