3

I need to use axios to send post request in order to upload files using Filepond Uploader.

How can i do it?

I'm using a custom process handler like below but it's not working.

processHandler: (fieldName, file, metadata, load, error, progress, abort) => {
        let formData = new FormData();
        let uploadPercentage = 0;
        formData.append('file', file);
        console.log(formData);

        HTTP.post('v1/upload', formData,
          {
            headers: {
              'Content-Type': 'multipart/form-data'
            },
            onUploadProgress: function (progressEvent) {
              uploadPercentage = parseInt(Math.round((progressEvent.loaded * 100) / progressEvent.total));
              console.log(uploadPercentage);
            }.bind(this)
          })
          .then((response) => {
            console.log(response);
            // Should call the load method when done and pass the returned server file id
            // the load method accepts either a string (id) or an object
            // the unique server file id is used by revert and restore functions
            load(response.data.data.id);
          })
          .catch((error) => {
            console.error(error);
            error("Has error");
          });

        // Should call the progress method to update the progress to 100% before calling load
        // Setting computable to false switches the loading indicator to infinite mode
        // (computable, processedSize, totalSize)
        progress(true, 0, 1024);

        // Should expose an abort method so the request can be cancelled
        return {
          abort: () => {
            // User tapped abort, cancel our ongoing actions here

            // Let FilePond know the request has been cancelled
            abort();
          }
        };
      }

I'm using this guide but it's not clear for me to understand how can i create upload and load process to handle my server response and request.

Thanks.

Boussadjra Brahim
  • 82,684
  • 19
  • 144
  • 164
Aminkt
  • 612
  • 9
  • 25

1 Answers1

15

FilePond author here.

I'm not entirely sure I understand the problem description but I'm going to try my best to help out. I've taken a quick peek at the Axios docs (https://github.com/axios/axios) and set up the following snippet.

{
    processHandler: (fieldName, file, metadata, load, error, progress, abort) => {

        // set data
        const formData = new FormData();
        formData.append('file', file, file.name);

        // related to aborting the request
        const CancelToken = axios.CancelToken;
        const source = CancelToken.source();

        // the request itself
        axios({
            method: 'post',
            url: 'v1/upload',
            data: formData,
            cancelToken: source.token,
            onUploadProgress: (e) => {
                // updating progress indicator
                progress(e.lengthComputable, e.loaded, e.total);
            }
        }).then(response => {
            // passing the file id to FilePond
            load(response.data.data.id)
        }).catch((thrown) => {
            if (axios.isCancel(thrown)) {
                console.log('Request canceled', thrown.message);
            } else {
                // handle error
            }
        });

        // Setup abort interface
        return {
            abort: () => {
                source.cancel('Operation canceled by the user.');
            }
        };
    };
}
Rik
  • 3,328
  • 1
  • 20
  • 23
  • It works as expected, just made some small changes, thank you! – Iulian Pinzaru Apr 18 '19 at 17:49
  • @Rik Maybe you can help me. Look at this : https://stackoverflow.com/questions/60219552/how-can-i-add-required-validation-on-filepond-vue – moses toh Feb 15 '20 at 23:15
  • How can I send files to the server? using file pond makes me a little confused. should I set :server property or :process property? Generally how server founds put the file? – ali Falahati May 25 '20 at 15:15
  • @aliFalahati Set `server` property to the API end point of your server, where your server handles requests containing file posts. You can also set a function in which case you need to set up a custom method to post files to your server, see: https://pqina.nl/filepond/docs/patterns/api/server/ – Rik May 28 '20 at 07:03
  • @Rik Seems you can help to answer this question https://stackoverflow.com/questions/73671336/why-label-of-file-size-on-filepond-show-1kb-every-file-when-displaying-edit-form – positive developer Sep 10 '22 at 13:26
  • This works. Just one thing, FormData doesn't work if you are using S3 presigned to upload files https://stackoverflow.com/questions/58234437/corrupted-image-on-uploading-image-to-aws-s3-via-signed-url. Simply replace `formData` with `file` in above code should work with s3 presigned url. – Yang Liu Dec 26 '22 at 21:08