16
Views
7
Comments
[ReactFilePondUpload] Validating duplicate filenames
Forge component by Stuart Harris
7
Published on 11 Aug 2020

Hi Stuart,

I was wondering if there is a way to validate the files that the user wants to upload, so they are not the same to the already uploaded files?

The first ideea I had was to put some kind of regex in the MustNotMatchFilenameRegex, but then the error message is confusing because it is related to the filetype being wrong.

My second approach would be to validate the file when the UploadStarted event is triggered, but then there is no way to cancel the upload so this will be even more confusing, because it will show the green success message, even if it's not supposed to.

Do you have any idea on another approach I should follow, for this?

(If it's not clear enough, I can write the whole use case, for a better understanding of the requirement)

Rank: #164

Hi Andrei,

There is no further way to validate an existing filename than you have already tried. Although a file with the same filename is not necessarily the same file.

The alternative is to allow the upload of the file, but handle the issue in the callback in your own code rather than making the upload plugin do it.  The upload plugin would then just handle uploading the file, then your processing code would handle the error.

I hope this helps.

Kind regards,

Stuart

Rank: #4537

Hi Stuart,

I agree that a same filename doesn't mean it's the same file, but for now it's enough.

Is there a way to change the error message that is shown when the MustNotMatchFilenameRegex is triggered, because now it shows the same message as when a filetype is not accepted and that is confusing for the user.

I tried to do as you said, and handle the issue in the callback in my own code, but this means the user is going to see the green message that the file was uploaded, and after that an error message, which can be pretty confusing.

Kind regards,

Andrei

Rank: #4537

A little small update,

I tried to build a regex in the following way: (filename\.extension)?(filename\.extension)? and so on, for all the files already existing and save it into a local variable that is passed to the Upload block in MustNotMatchFilenameRegex.

The problem is that it still allows files with the same name to upload. I tested my regex on an online tester and it seems to work. Do you have any idea why this is not working?

Rank: #4537

Hi Stuart,

Do you have any idea why the regex doesn't work in the FileUpload widget, but if I test it separately, it works well?

Kind regards,

Andrei

Rank: #164

Hi Andrei,

The regex works in the FileUpload widget with a JavaScript format regular expression.  Note that regular expressions for .NET and JavaScript (and other environments) are not the same.

Try checking your regular expression on this site https://regex101.com/

The regular expression you posted does not appear to match.  The question mark only handles the previous character. Try using |, like this:

(filename\.extension)|(filename\.extension)

I hope this helps!

Kind regards,

Stuart

Rank: #4537

Hi Stuart,

I changed the regex and it still didn't want to work.
I debugged the JS that is doing the actual comparison for the regex and I found what the problem was.
I am updating the variable that is used for the MustNotMatchFilenameRegex dynamically (according to the elements in a list) but I saw that, unfortunately, this update doesn't get to the script.

So it's not working because after the first initialization of the variable, the regex is changed but the one used in the script remains the initial one.

Do you have any solutions for this, or the only way would be for me to figure out a way to initialize the UploadBlock everytime?



Thanks for all the help until now!

Kind regards,
Andrei

Rank: #77793

The first ideea I had was to put some kind of regex in the MustNotMatchFilenameRegex, but then the error message is confusing because it is related to the filetype being wrong.