Skip to content

Commit

Permalink
Allow formData to be transformed async for each file
Browse files Browse the repository at this point in the history
  • Loading branch information
gugiserman committed Oct 26, 2021
1 parent 7801f65 commit a3da7c2
Show file tree
Hide file tree
Showing 3 changed files with 4,261 additions and 2,600 deletions.
14 changes: 13 additions & 1 deletion src/dropzone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ export default class Dropzone extends Emitter {
// This function actually uploads the file(s) to the server.
// If dataBlocks contains the actual data to upload (meaning, that this could either be transformed
// files, or individual chunks for chunked upload).
_uploadData(files, dataBlocks) {
async _uploadData(files, dataBlocks) {
let xhr = new XMLHttpRequest();

// Put the xhr object in the file objects to be able to reference it later.
Expand Down Expand Up @@ -1434,9 +1434,21 @@ export default class Dropzone extends Emitter {

// Let the user add additional data if necessary
for (let file of files) {
const transformFileFormData = this.options.transformFileFormData

if (typeof transformFileFormData === "function") {
await transformFileFormData(file, formData)
}

this.emit("sending", file, xhr, formData);
}
if (this.options.uploadMultiple) {
const transformFilesFormData = this.options.transformFilesFormData

if (typeof transformFilesFormData === "function") {
await transformFilesFormData(files, formData)
}

this.emit("sendingmultiple", files, xhr, formData);
}

Expand Down
14 changes: 14 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,20 @@ let defaultOptions = {
}
},

/**
* Can be used to transform (usually append to) the formData for a specific file (for example, add S3 presigned URL data).
* Gets the `file` as the first parameter, and `formData` as the second parameter.
*/
async transformFileFormData(file, formData) {
// const something = await fetchSomethingElse(file.name)
// formData.append('something', something)
},

async transformFilesFormData(files, formData) {
// const something = await fetchSomethingElse()
// formData.append('something', something)
},

/**
* A string that contains the template used for each dropped
* file. Change it to fulfill your needs but make sure to properly
Expand Down

0 comments on commit a3da7c2

Please sign in to comment.