4

I have implemented Filepond uploaded in my page. When the user selects a file, I set that file on a html canvas for edits. However when the user wants to upload another file, filepond input retains last uploaded file.

I have tried FilePond.destroy(inputElement); after the file is successfully set on the canvas in the FilePond:addfile event.

 $('.upload-file').filepond();

$.fn.filepond.registerPlugin(
    FilePondPluginFileValidateType,
    FilePondPluginFileValidateSize,
    FilePondPluginImageResize,
    FilePondPluginImageExifOrientation,
    FilePondPluginImagePreview,
    FilePondPluginImageTransform,
    FilePondPluginImageCrop,
    FilePondPluginImageValidateSize,
);


     FilePond.setOptions({
      labelIdle: 'Drag & Drop your file or <span class="filepond--label- 
       action"> Browse </span>',
    maxFiles: 1,
    allowDrop: true,
    allowMultiple: false,
    dropOnPage: true, //for page to work, element has to be false https://github.com/pqina/filepond/issues/113
    dropOnElement: false,
    labelTapToCancel: '', //we dont want to allow cancel
    labelTapToUndo: '',
    maxFileSize: intFileSizeInMB,
    allowReplace: true,
    allowRevert: false,
    instantUpload: false 
});



const pond = document.querySelector('.filepond--root');

pond.addEventListener('FilePond:addfile', e => {

    console.log('File added', e.detail);

    if (e.detail) {
        if (e.detail.file && e.detail.file.file.name) {
            SetFileOnCanvas(e.detail.file.file, e.detail.file.file.name);

            const inputElement = document.querySelector('input[type="file"]');
            FilePond.destroy(inputElement); 
        }
    }
});



pond.addEventListener('FilePond:processfile', e => {
    console.log('Process File COMPLETE', e.detail);
});

After a file is uploaded and set to Canvas the file upload input should be cleared and ready for another upload.

4
  • Can you not call removeFile? To remove the item when the upload is done? Commented Jun 3, 2019 at 8:49
  • I tried doing that, 'pond.removeFile' inside the FilePond:addfile event, but pond object instance is not available there. Commented Jun 3, 2019 at 13:26
  • 3
    $('.upload-file').filepond('removeFile'); Something like this should work if you're using the jQuery adapter. Commented Jun 3, 2019 at 13:57
  • 1
    Adding to @Rik's comment: for multiple files, use $('.upload-file').filepond('removeFiles'); -- note plural removeFiles Commented Mar 30, 2023 at 15:28

4 Answers 4

2

Working solution

var pond_ids = [];

if (pond.getFiles().length != 0) {  // "pond" is an object, created by FilePond.create 
    pond.getFiles().forEach(function(file) {
        pond_ids.push(file.id);
    });
}

pond.removeFiles(pond_ids);
Sign up to request clarification or add additional context in comments.

Comments

0

After upload file "Complete function"

you can do like this (simple example):

if (filePond.getFiles().length != 0) {
    for (var i = 0; i <= filePond.getFiles().length - 1; i++) {
      filePond.removeFile(filePond.getFiles()[0].id)
    }
  }

Comments

0

I know its too late but you can use

let filePond = FilePond.find(document.getElementById(filePondElementId));
    if (filePond != null) {
    //this will remove all files
        filePond.removeFiles();
    }
<script src="https://unpkg.com/filepond/dist/filepond.min.js"></script>

Comments

0

Assuming that you create your filepond instance through function create_pondProfile and your input has class filepond, in you filepond config in server block do like this:

server: {
url: '',
process: {
    url: '/path/to/upload/',
    headers: {'X-CSRF-TOKEN': csrf},
    ondata: (formData) => {
        return formData;
    },
    onload: (response) => {
        FilePond.destroy(document.querySelector('.filepond'));
        create_pondProfile('.filepond');
    }
},
...
...
}

It will destroy current instance of filepond and creates new one after upload.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.