All Projects β†’ greena13 β†’ react-simple-file-input

greena13 / react-simple-file-input

Licence: ISC license
Simple wrapper for the HTML input tag and HTML5 FileReader API

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-simple-file-input

file-upload-with-preview
πŸ–Ό Simple file-upload utility that shows a preview of the uploaded image. Written in TypeScript. No dependencies. Works well with or without a framework.
Stars: ✭ 406 (+1300%)
Mutual labels:  input, upload, file
PHP-FileUpload
Simple and convenient file uploads β€” secure by default
Stars: ✭ 53 (+82.76%)
Mutual labels:  input, upload, file
File Upload With Preview
πŸ–Ό A simple file-upload utility that shows a preview of the uploaded image. Written in pure JavaScript. No dependencies. Works well with Bootstrap 4 or without a framework.
Stars: ✭ 352 (+1113.79%)
Mutual labels:  input, upload, file
react-file-input-previews-base64
This package provides an easy to use, ready to go and customizable wrapper around file input, with option for image previews and returning file as base64 string.
Stars: ✭ 15 (-48.28%)
Mutual labels:  input, upload, file
Ngx Material File Input
File input for Angular Material form-field
Stars: ✭ 193 (+565.52%)
Mutual labels:  input, upload, file
Angular File Uploader
Angular file uploader is an Angular 2/4/5/6/7/8/9/10 + file uploader module with Real-Time Progress Bar, Responsive design, Angular Universal Compatibility, localization and multiple themes which includes Drag and Drop and much more.
Stars: ✭ 92 (+217.24%)
Mutual labels:  upload, file
Laravel Filemanager Example 5.3
Demo integration for laravel-filemanager (https://github.com/UniSharp/laravel-filemanager).
Stars: ✭ 100 (+244.83%)
Mutual labels:  upload, file
svelte-filepond
πŸ”Œ A handy FilePond adapter component for Svelte
Stars: ✭ 188 (+548.28%)
Mutual labels:  upload, file
Uploadcare Widget
Uploadcare Widget, an ultimate tool for HTML5 file upload supporting multiple file upload, drag&drop, validation by file size/file extension/MIME file type, progress bar for file uploads, image preview.
Stars: ✭ 183 (+531.03%)
Mutual labels:  upload, file
Fileuploaderplugin
Simple cross platform plugin to upload files.
Stars: ✭ 59 (+103.45%)
Mutual labels:  upload, file
Jquery Filepond
πŸ”Œ A handy FilePond wrapper for jQuery
Stars: ✭ 124 (+327.59%)
Mutual labels:  upload, file
Publicleech
can be found on Telegram as https://telegram.dog/PublicLeechGroup
Stars: ✭ 236 (+713.79%)
Mutual labels:  upload, file
Vue Filepond
πŸ”Œ A handy FilePond adapter component for Vue
Stars: ✭ 1,263 (+4255.17%)
Mutual labels:  upload, file
Nodestream
Storage-agnostic streaming library for binary data transfers
Stars: ✭ 70 (+141.38%)
Mutual labels:  upload, file
Laravel Filemanager
Media gallery with CKEditor, TinyMCE and Summernote support. Built on Laravel file system.
Stars: ✭ 1,688 (+5720.69%)
Mutual labels:  upload, file
Laravel Simple Uploader
Simple file uploader for Laravel 5.
Stars: ✭ 59 (+103.45%)
Mutual labels:  upload, file
React Files
A file input (dropzone) management component for React
Stars: ✭ 126 (+334.48%)
Mutual labels:  upload, file
File Dialog
Trigger the upload file dialog directly from your code easily.
Stars: ✭ 51 (+75.86%)
Mutual labels:  upload, file
Angular Filepond
πŸ”Œ A handy FilePond adapter component for Angular
Stars: ✭ 59 (+103.45%)
Mutual labels:  upload, file
react-magic-dropzone
✨Magically drag and drop files/links for uploading
Stars: ✭ 11 (-62.07%)
Mutual labels:  input, upload

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].