flowjs / flow.js

A JavaScript library providing multiple simultaneous, stable, fault-tolerant and resumable/restartable file uploads via the HTML5 File API.
Other
2.96k stars 346 forks source link

how do i check the file before upload? #271

Open yuanyuanweiwu opened 4 years ago

yuanyuanweiwu commented 4 years ago

Now I have a requirement to call the background interface to detect before uploading the file. If the file already exists, cancel the upload and prompt, no upload file. Which method or configuration property do I use to perform this judgment? Thank you

MulderNick commented 4 years ago

you can use the events "fileAdded" and "filesAdded"

const flow = new Flow() flow.on("filesAdded", function(flowFiles) { // ToDo implement duplication check });

you can check the files there, remove all duplicated files from the flow instance via flowFile.cancel() and then after that start the upload with flow.resume() or flow.upload()

madsnow commented 3 years ago

Is it possible to perform an async operation in fileAdded?

AidasK commented 3 years ago

No, it's not. You have to reject a file and add it later instead

drzraf commented 3 years ago

Highly related to the discussion initiated at https://github.com/flowjs/flow.js/issues/319

The last comment from madsnow was look for an async filter withing the general processing of filesAdded. I can't figure how this could be done correctly and don't think this should be supported (at least for now).

drzraf commented 3 years ago

I plan to add a preFilterFiles hook (filesAdded staying as a simple notification event), in a future PR. This hook would expect a Promise to be returned and block further processing (the call to addFiles, (async too)) until the hook's promise returns.

I don't know if it's the best JS-development pattern, but it'd resolve that problem (and the one caused by fileAdded) using a reproducible pattern and avoid adding state variables as Flow or FlowFile properties.

AidasK commented 3 years ago

@drzraf sounds awesome 👍

drzraf commented 3 years ago

@madsnow : You could have a look at https://github.com/flowjs/flow.js/pull/329 and see if

await asyncAddFile(yourfile, null, async (flowFile, event) => {
 // whatever you want before initialization
});

would fit (where the 3rd parameter is an async equivalent of initFileFn).

Note: