1.0.8 • Published 6 years ago

reactjs-file-uploader v1.0.8

Weekly downloads
676
License
MIT
Repository
github
Last release
6 years ago

ReactJS File Uploader

A flexible React component for uploading files. Supports multiple files, progress feedback and upload / abort controls.

View Live Demo

Features

  • Multiple files
  • Progress feedback
  • Upload / abort controls

Example

class VanillaExample extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            files: [],
        };
        this.uploadFiles = this.uploadFiles.bind(this);
        this.uploadFile = this.uploadFile.bind(this);
    }

    render() {
        return (
            <div>
                <input
                    type="file"
                    accept="image/*"
                    onChange={event => this.setState({files: this.state.files.concat(Array.from(event.target.files))})}
                    multiple
                />
                <FileManager
                    files={this.state.files}
                >
                    {this.uploadFiles}
                </FileManager>
            </div>
        )
    }

    uploadFiles(files) {
        return files.map(this.uploadFile);
    }

    uploadFile(file) {
        return (
            <FileUploader
                key={file.key}
                file={file}
                url='https://api.cloudinary.com/v1_1/dpdenton/upload'
                formData={{
                    file,
                    upload_preset: 'public',
                    tags: 'vanilla',
                }}
                readFile
            >
                {this.fileProgress}
            </FileUploader>
        )
    }

    static fileProgress({
    
        /*
        References to the Event objects.
        Initial state is null and each propert gets assigned on Event.
         */
        uploadReady,
        uploadStart,
        uploadProgress,
        uploadComplete,
        downloadStart,
        downloadProgress,
        downloadComplete,
        error,
        abort,
        timeout,

        /*
        The sequential state of the request
        enum {
            uploadReady, uploadStart, uploadProgress, uploadComplete, downloadStart
            downloadStart, downloadProgress, downloadComplete
        }
         */
        requestState,

        /*
        Function references to start / abort request
          */
        startUpload,
        abortRequest,

        /*
        Request Object reference (XMLHttpReqeust)
         */
        request,

        /*
        Response text Object (JSON)
         */
        response,

        /*
        Data of the file being uploaded (if readData props is true)
         */
        fileData,

     }) {
        return (
            <div>
                {fileData && <img src={fileData} width={200} alt="Preview"/>}
                {startUpload && <button onClick={startUpload}>Upload File</button>}
                {requestState && requestState}
            </div>
        )
    }

}

Options FileUploader

PropertyTypeDefaultDescription
children(required)func: (state) => React.NodeReturns state of FileUploader instance. See above example for state structure.
file(required)object: FileFile objects are generally retrieved from a FileList object returned as a result of a user selecting files using the element, from a drag and drop operation's DataTransfer object, or from the mozGetAsFile() API on an HTMLCanvasElement.
url(required)stringUpload url endpoint.
formDataobject: { key: value }{}key: value formData to be sent with the request. If sending the file this needs to be explicit e.g. formData={{file_field: file}}
autoUploadboolfalseIf true immediately start upload when file is passed to Component.
readFileboolfalseIf true provides a reference to the file data in the component state returned by children
onUploadReadyfunc: (XMLHttpRequest) => void() => {}Hook to uploadReady event.
onUploadStartfunc: (ProgressEvent) => void() => {}Hook to uploadStart event.
onUploadProgressfunc: (ProgressEvent) => void() => {}Hook to uploadProgress event.
onUploadCompletefunc: (ProgressEvent) => void() => {}Hook to uploadComplete event.
onDownloadStartfunc: (Event) => void() => {}Hook to downloadStart event.
onDownloadCompletefunc: (Event) => void() => {}Hook to downloadComplete event.
onErrorfunc: (Event) => void() => {}Hook to error event.
onAbortfunc: (Event) => void() => {}Hook to abort event.
onTimeoutfunc: (Event) => void() => {}Hook to timeout event.
onFileDataReadyfunc: (Event) => void() => {}Hook to fileDataReady event.

Options FileManager

The FileManager is an optional HOC that manages the files that are sent to the FileUploader.

It ensures no duplicate are sent to be uploaded and provides each file a unique key attribute.

PropertyTypeDefaultDescription
children(required)func: (file) => React.NodeReturns File from Array of File objects.
files(required)array: FileArray or FileList of File objects.

License

MIT

1.0.8

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.2

6 years ago

1.0.0

6 years ago