A simple skeleton for building an awesome uploader component.
# npm
npm install react-uploader-skeleton
# yarn
yarn add react-uploader-skeleton
<ReactUploaderSkeleton
onFinish={e => console.log('onFinished', e)}
onFileChange={e => console.log('onFileChange', e)}
request={(fileData, onProgress, onError, onSuccess) => {
const xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', e => {
const done = e.loaded;
const total = e.total;
const progress = done / total;
if (progress > 1) {
onProgress(1);
} else {
onProgress(done / total);
}
});
xhr.addEventListener('readystatechange', () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
onSuccess(xhr.response);
} else {
onError();
}
}
});
xhr.open('POST', 'http://127.0.0.1:3000/file');
const formData = new FormData();
formData.append('file', fileData.fileData as File);
xhr.send(formData);
}}
/>
TYPE
number;
COMMENT
How many file uploads to process in parallel.
TYPE
(files: IUploaderFileData[]) => void
COMMENT
Called when files change.
TYPE
(files: IUploaderFileData[]) => void
COMMENT
Called when the upload is complete.
request: (
uploaderFileData: IUploaderFileData,
onProgress: (percent: number) => void,
onError: (message?: string) => void,
onSuccess: (url?: string) => void
) => void;
COMMENT
Custom upload request.
TYPE
React.ReactNode;
COMMENT
Placeholders when there is no file upload.
interface IUploaderFileData {
name: string; // name of file
state: string; // file state ["resolved","error","waiting","uploading"]
url?: string; // link to file in cloud
fileData?: File; // raw file data
progress?: number; // progress of upload
}