
Security News
Frontier AI Is Now Critical Infrastructure
The Fable shutdown shows how quickly model access can become a business continuity risk for AI-dependent engineering teams.
react-simple-file-input
Advanced tools
Simple wrapper for the HTML input tag and HTML5 FileReader API that supports multiple files
<FileInput
readAs='binary'
multiple
onLoadStart={this.showProgressBar}
onLoad={this.handleFileSelected}
onProgress={this.updateProgressBar}
cancelIf={fileIsIncorrectFiletype}
abortIf={this.cancelButtonClicked}
onCancel={this.showInvalidFileTypeMessage}
onAbort={this.resetCancelButtonClicked}
/>
npm install react-simple-file-input --save
react-simple-file-input expects a readAs option to indicate how the file should be read. Valid options are:
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.
react-simple-file-input supports the following event handlers:
Each receives the native event as the first argument, and the selected file object as the second.
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.
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) 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 most be done by maintaining state externally, in your handlers.
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.
react-simple-file-input provides two props for canceling and aborting file reads
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.
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 as arguments, respectively and if it returns a truthy value it aborts the file read and calls the onAbort handler. The onProgress event is not called if the file read aborts.
React simple file input does not support children. If you wish to use another element to set the clickable area for the user, use labels or a similar strategy (see example below).
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 label as demonstrated below in the example.
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});
}
}
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.
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.
FAQs
Simple wrapper for the HTML input tag and HTML5 FileReader API
The npm package react-simple-file-input receives a total of 15,640 weekly downloads. As such, react-simple-file-input popularity was classified as popular.
We found that react-simple-file-input demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.

Security News
The Fable shutdown shows how quickly model access can become a business continuity risk for AI-dependent engineering teams.

Security News
AI agents are pulling packages into environments no scanner is watching, creating exposure before security teams can see it.

Security News
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.