ReactJS File Uploader
A flexible React component for uploading files. Supports multiple files, progress feedback and upload / abort controls.
Features
- Multiple files
- Progress feedback
- Upload / abort controls
Example
Component { ; thisstate = files: ; thisuploadFiles = thisuploadFiles; thisuploadFile = thisuploadFile; } { return <div> <input ="file" ="image/*" = /> <FileManager = > thisuploadFiles </FileManager> </div> } { return files; } { return <FileUploader = = ='https://api.cloudinary.com/v1_1/dpdenton/upload' = > thisfileProgress </FileUploader> } static return <div> fileData && <img = = ="Preview"/> startUpload && <button =>Upload File</button> requestState && requestState </div>
Options FileUploader
Property | Type | Default | Description |
---|---|---|---|
children (required) |
func: (state) => React.Node | Returns state of FileUploader instance. See above example for state structure. | |
file (required) |
object: File | File 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) |
string | Upload url endpoint. | |
formData | object: { 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}} |
autoUpload | bool | false |
If true immediately start upload when file is passed to Component. |
readFile | bool | false |
If true provides a reference to the file data in the component state returned by children |
onUploadReady | func: (XMLHttpRequest) => void | () => {} | Hook to uploadReady event. |
onUploadStart | func: (ProgressEvent) => void | () => {} | Hook to uploadStart event. |
onUploadProgress | func: (ProgressEvent) => void | () => {} | Hook to uploadProgress event. |
onUploadComplete | func: (ProgressEvent) => void | () => {} | Hook to uploadComplete event. |
onDownloadStart | func: (Event) => void | () => {} | Hook to downloadStart event. |
onDownloadComplete | func: (Event) => void | () => {} | Hook to downloadComplete event. |
onError | func: (Event) => void | () => {} | Hook to error event. |
onAbort | func: (Event) => void | () => {} | Hook to abort event. |
onTimeout | func: (Event) => void | () => {} | Hook to timeout event. |
onFileDataReady | func: (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.
Property | Type | Default | Description |
---|---|---|---|
children (required) |
func: (file) => React.Node | Returns File from Array of File objects. | |
files (required) |
array: File | Array or FileList of File objects. |
License
MIT