Skip to content

FEAT: Gives the option to send all files in a single request #32 #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/Dropzone/components/dropzone/Dropzone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const Dropzone: React.FC<DropzoneProps> = (props: DropzoneProps) => {
//uploading
uploadConfig,
fakeUpload,
groupUpload,
onUploadStart,
onUploadFinish,
//styling
Expand Down Expand Up @@ -326,6 +327,46 @@ const Dropzone: React.FC<DropzoneProps> = (props: DropzoneProps) => {

//return;
let serverResponses: Array<ExtFile> = [];

if(groupUpload) {
const unifiedUpload = (method, url, arrOfFiles) : Promise<{ success : boolean, message : string, payload: object }> => {
arrOfExtFilesInstances.forEach((el) => el.uploadStatus = "uploading");
handleFilesChange(sanitizeArrExtFile(arrOfExtFilesInstances), true);
const formData = new FormData();
for (let i=0; i < arrOfFiles.length; i++) {
formData.append('files', arrOfFiles[i].file )
}
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.upload.onprogress = (e) => {arrOfExtFilesInstances.forEach((el) => { el.progress = (e.loaded / e.total) * 100 });handleFilesChange(sanitizeArrExtFile(arrOfExtFilesInstances), true)};
xhr.responseType = 'json'
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
console.log(xhr.response);
console.log(typeof xhr.response);
resolve(xhr.response);
} else {
reject(xhr.response);
}
};
xhr.onerror = (err) => {
reject(err);
};
xhr.open(method, url);
xhr.send(formData)
});
};
try {
let respo:{ success : boolean, message : string, payload: object} = await unifiedUpload("POST", url, arrOfExtFilesInstances);
arrOfExtFilesInstances.forEach( el => el.uploadStatus = "success");
arrOfExtFilesInstances.forEach( el => el.uploadMessage = respo.message);
} catch (err) {
arrOfExtFilesInstances.forEach( el => el.uploadStatus = "error");
arrOfExtFilesInstances.forEach( el => el.uploadMessage = err.message);
console.log(err)
}
handleFilesChange(sanitizeArrExtFile(arrOfExtFilesInstances), true);
} else {
//Uplad files one by one
for (let i = 0; i < arrOfExtFilesInstances.length; i++) {
const currentExtFileInstance: ExtFileInstance = arrOfExtFilesInstances[i];
Expand Down Expand Up @@ -405,7 +446,7 @@ const Dropzone: React.FC<DropzoneProps> = (props: DropzoneProps) => {
handleFilesChange(sanitizeArrExtFile(arrOfExtFilesInstances), true);
}
}

}
setLocalFiles(sanitizeArrExtFile(arrOfExtFilesInstances));

// upload group finished :D
Expand Down
5 changes: 5 additions & 0 deletions src/Dropzone/components/dropzone/DropzoneProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ export interface DropzoneFullProps extends OverridableComponentProps {
* the upload button
*/
fakeUpload?: boolean;
/**
* If set, will upload all loaded files in a single multipart request appending array of files
* to the FormData files key
*/
groupUpload?: boolean;
/**
* Callback fired when the upload process starts.
*/
Expand Down