Survey-library: no way to prevent from adding files with the same name in the same question

Created on 3 Feb 2020  路  9Comments  路  Source: surveyjs/survey-library

Are you requesting a feature, reporting a bug or asking a question?

feature/bug

What is the current behavior?

If I try to add the same file (or simply a file with the same name) to a multiple files question there's is no way to identify this situation. Problem actually rise when try to delete one of the file: _onClearFiles_ callback receive filename so you end up to delete all files with that name

What is the expected behavior?

I think that totally preventing this is acceptable: you cannot add files with the same name to the same question. This would solve. Otherwise, in order to run custom checks, I would need to receive question context (along with its current value) in _onUploadFiles_ callback which may be more complex

How would you reproduce the current behavior (if this is a bug)?

try to add the same file in two different steps (which does not make any sense but you can) and then remove one of them
https://plnkr.co/edit/W7dl9NtGVxrfzApgSaUJ?p=preview

question

Most helpful comment

You don't need to create a real File object to pass a result.

Instead of

                const newFilename = (new Date).getTime().toString() + file.name 

                return {
                    file: new File([file], newFilename, { type: file.type }),
                    content: 'path/to/' + newFilename
                };

you can write

                const newFilename = (new Date).getTime().toString() + file.name 

                return {
                    file: ( name: newFilename, type: file.type }),
                    content: 'path/to/' + newFilename
                };

All 9 comments

The onUploadFiles event handler makes a callback to your server side. The server-side method can rename passed file (e.g. to GUID as we do in our surveyjs.io service) and pass this GUID to the client side. Thus instead of content: 'path/to/' + file.name will be something like content: 'path/to/' + fileUniqueID. During the clear operation this fileUniqueID will be used to address exactly this file in a server storage.

This is how we solve the task of processing files with the same name in our service (https://surveyjsio-stage.azurewebsites.net/create-survey/)

yes I also add a timestamp as suffix to the filename. But even if you rename the returned content when you clear only one of the uploaded homonym files, surveyJS will remove both. In the following plunk the content is renamed so if you try to upload the same file twice the content is different but the filename is still the same. So if you try to delete only one file both will be deleted.

https://plnkr.co/edit/mAdmMiadZwFRDtSqwRER?p=preview

Sorry if I am still missing something

No, we rename every file posted on our server and return both file name and file guid. In the content we use the guid. Thus every uploaded file has the unique id.

We change file name to guid in our server side storage and address the file by this guid.

so at line 35 of the last plunk returned _file_ prop already has the guid name?

yes, exactly

My storage service (minio/s3) returns 204, so no way to return the actual stored file. At the same time the Javascript File object received as param from the browser is only read, cannot change the name property. I would need a way to build on-the-fly a new Javascript File object with the right name. Any suggestion?

maybe found

https://plnkr.co/edit/n5PBivtnFij5PxxMspDj?p=preview

I'll try in my app

thanks a lot

You don't need to create a real File object to pass a result.

Instead of

                const newFilename = (new Date).getTime().toString() + file.name 

                return {
                    file: new File([file], newFilename, { type: file.type }),
                    content: 'path/to/' + newFilename
                };

you can write

                const newFilename = (new Date).getTime().toString() + file.name 

                return {
                    file: ( name: newFilename, type: file.type }),
                    content: 'path/to/' + newFilename
                };
Was this page helpful?
0 / 5 - 0 ratings

Related issues

xiuzhen1103 picture xiuzhen1103  路  4Comments

dmitrykurmanov picture dmitrykurmanov  路  3Comments

ian-emsens-sb picture ian-emsens-sb  路  3Comments

AWIXOR picture AWIXOR  路  3Comments

GuiAfonso picture GuiAfonso  路  4Comments