Search code examples
jqueryfilepond

File pond prevent duplicate file upload


Hello I am using filepond for uploading files from here https://pqina.nl/filepond/docs/getting-started/examples/

I have explored the documentation and from the answer here I tried few

Prevent same files/photos select

 var pond;
 pond = FilePond.create(
        document.querySelector('input.filepond')
    );

I tried triggering the removefile and see if I am getting any files to it as follows

pond.on('removefile', function (file) {
        alert(file);
    });

But this return null, I tried this logic in

pond.on('addfile',
        function (error, file) {
        var fileCnt = pond.getFiles();
 });

This gives me the count but I am trying to handle something like before file upload event if anything is there. Or is there any alternate file control which behaves the same please suggest.


Solution

  • OK I came up with this approach in addition to this answer here

    Prevent same files/photos select

    var pond;
    var filenames = [];
    pond = FilePond.create(
     document.querySelector('input.filepond')
    );
    
    pond.on('removefile', function (err, f) {
       filenames = [];
       var fileCnt = pond.getFiles().length;
       for (var fCnt = 0; fCnt < fileCnt; fCnt++) {
          var file = pond.getFile(fCnt);
          filenames.push(file.filename);
        }
     });
    
    pond.on('addfile',
      function (error, file) {
        if (filenames.includes(file.filename)) {
            var fileError = "A file with name " + file.filename + " was already uploaded";
            alert(fileError);
            handleFileError(file);
          }
          if (error) handleFileError(error, file);
           filenames.push(file.filename);
        });
        function handleFileError(file) {
            pond.removeFile(file);
        }
    

    Fiddle for the same https://jsfiddle.net/DorababuMeka/ydj2aruL/