xmcx
xmcx

Reputation: 346

how can i forbid the same file use primefaces fileupload with jsf

I am using PrimeFaces with JSFin web,and I don't want user upload the same file.That's like you can't save the same file in a path. Is there any way to achieve it?

Upvotes: 1

Views: 486

Answers (1)

Vsevolod Golovanov
Vsevolod Golovanov

Reputation: 4206

You can't know user's local file path: see this. Also a user could rename or move files, and circumvent the file path check. As Kukeltje commented, better compare file content server-side.

If you really want to validate client-side based on file names, then you could override the widget's validate method. It's undocumented though, so it can go away in future PrimeFaces versions.

<h:form>
    <p:fileUpload mode="advanced" widgetVar="fileup" />
    <script>
        $(function() {
            PF('fileup').validate = function(file) {
                if (file.name === 'alreadyuploaded.png')
                    return 'Not this one though'; // return a error message string
                else
                    return null; // allow
            }
        });
    </script>
</h:form>

I didn't test this in older non-HTML5 browsers though - p:fileUpload in advanced mode works differently in those.

Upvotes: 1

Related Questions