Security News
Research
Supply Chain Attack on Rspack npm Packages Injects Cryptojacking Malware
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
react-file-uploader
Advanced tools
react-file-uploader is a set of highly customizable React components that helps you to build a file uploader in your application easily.
react-file-uploader is not production ready because it is not fully tested on all browsers, and the unit test coverage is just not good enough. However, it has been used in my cms project since v0.3.0.
To install:
npm install --save react-file-uploader
this module currently contains 4 major entities, which are
Receiver
helps you to manage the Drag and Drop functionality. Once you mounted the Receiver
component, your application will start listen to dragenter
, dragover
, dragleave
and drop
events.
import { Receiver } from 'react-file-uploader';
<Receiver
customClass={STRING_OR_ARRAY}
style={OBJECT}
isOpen={BOOLEAN}
onDragEnter={FUNCTION}
onDragOver={FUNCTION}
onDragLeave={FUNCTION}
onFileDrop={FUNCTION}
>
<div>
visual layer of the receiver (drag & drop panel)
</div>
</Receiver>
string | array
: the class name(s) for the div
wrapperobject
: the style for the div
wrapperboolean
required
: to control in the parent component whether the Receiver is visble.function
required
: when isOpen
is false
, this will be fired with the window event of dragenter
once .You may make use of the drag & drop event callbacks.
// @param e Object DragEvent
function onDragEnter(e) {
this.setState({ isReceiverOpen: true });
}
function
: this will be fired with the window event of dragover
.// @param e Object DragEvent
function onDragOver(e) {
// your codes here
}
function
required
: when the drag event entirely left the client (i.e. dragLevel === 0
), this will be fired with the window event of dragleave
once.// @param e Object DragEvent
function onDragLeave(e) {
this.setState({ isReceiverOpen: false });
}
function
required
: this will be fired with the window event of drop
. You may execute any validation / checking process here i.e. size, file type, etc.// @param e Object DragEvent
// @param files Array the files dropped on the target node
function onFileDrop(e, files) {
// check if the files are drop on the targeted DOM
const node = ReactDOM.findDOMNode(this.refs.uploadPanel);
if (e.target !== node) {
return;
}
files.forEach(file => {
// check file size
if (file.size > 1000 * 1000) {
file.error = 'file size exceeded 1MB';
}
})
// put files into state/stores by setState/action
this.setState({
files: this.state.files.concat(files),
});
// close the Receiver after file dropped
this.setState({ isReceiverOpen: false });
}
Upload Manager serves as a high order component which helps you to manage the upload related parameters and functions. It prepares the upload function with superagent
the children elements, and helps you to update the lifecycle status of the uploading files.
**IMPORTANT: this high order component serves only for ** UploadHandler
import { UploadManager } from 'react-file-uploader';
<UploadManager
customClass={STRING_OR_ARRAY}
style={OBJECT}
uploadUrl={STRING}
uploadHeader={OBJECT}
onUpload={FUNCTION}
onUploadProgress={FUNCTION}
onUploadEnd={FUNCTION}
>
// UploadHandler as children
files.map(file => (
<UploadHandle key={file._id} file={FILE_INSTANCE} autoStart={BOOLEAN} />
))
</UploadManager>
string
: the DOM tag name of the wrapperstring | array
: the class name(s) for the wrapperobject
: the style for the wrapperstring
required
: the url of the upload end point from your server.object
: the header object to be set as request header.function
: this will be fired when the POST
request is just sent.// @param file Object the file object returned with UPLOADING status and 0% progress.
function onUploadStart(file) {
// your codes here
}
function
: this will be fired when the POST
request returns progress. A debounced function is STRONGLY recommaned to prevent from performance issue.// @param file Object the file object returned with UPLOADING status and n% progress.
function onUploadProgress(file) {
// your codes here
}
function
required
: this will be fired upon the end of POST
request.// @param file Object the file object returned with either UPLOADED or FAILED status and 100% progress. When it is wilh FAILED status, error property should be also assigned to the file object.
function onUploadEnd(file) {
// your codes here
}
function
: this function is to process the arguments of (err, res)
in superagent.end()
. In this function, you can resolve the error and result according to your upload api response. Default implementation is available as defaultProps.function errorHandler(err, res) {
let error = null;
const body = clone(res.body);
if (err) {
error = err;
} else if (body && body.errors) {
error = body.errors;
}
delete body.errors;
return { error, result: body };
}
Upload Handler helps you to execute the upload lifecycle, which is start
, progress
and end
. It also acts as the presentation layer of a file, showing users the info of the uploading / uploaded file.
import { UploadHandler } from 'react-file-uploader';
<UploadHandler
customClass={CLASS_STRING_OR_ARRAY}
style={STYLE_OBJECT}
file={FILE_OBJECT}
autoStart={BOOLEAN}
upload={UPLOAD_FUNCTION}
/>
component
- string
: the DOM tag name of the wrapper
customClass
- string | array
: the class name(s) for the wrapper
style
- object
: the style for the wrapper
file
- object
required
: the file object that is uploaded / going to be uploaded.
autoStart
- boolean
: when autoStart
is set to true
, upon the UploadHandler component did mount, it will detect if the file i.e. as props is with the PENDING
status and initialise a POST
request which is sent to the uploadUrl
you passed to the UploadManager
.
upload
- function
: the function that contains the upload logic, you may pass it directly when you are using UploadHandler
alone, or it could be prepared by UploadManager
.
// @param url String API upload end point
// @param file Object File Object
function upload(url, file) {
// your codes here
}
react-file-uploader
defines a set of status constants to represent the file status. The corresponding status will be assign to a file object throughout the uploading life cycle.
FAILED = -1
PENDING = 0
UPLOADING = 1
UPLOADED = 2
MIT
FAQs
A set of file-upload-components with React.js.
The npm package react-file-uploader receives a total of 194 weekly downloads. As such, react-file-uploader popularity was classified as not popular.
We found that react-file-uploader 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
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.
Security News
Sonar’s acquisition of Tidelift highlights a growing industry shift toward sustainable open source funding, addressing maintainer burnout and critical software dependencies.