【问题标题】:How to use Filepond in vue js and to upload files using axios?如何在vue js中使用Filepond并使用axios上传文件?
【发布时间】:2019-02-12 16:32:27
【问题描述】:

我需要使用 axios 发送 post 请求才能使用 Filepond Uploader 上传文件。

我该怎么做?

我正在使用如下所示的自定义流程处理程序,但它不起作用。

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();
          }
        };
      }

我正在使用this guide,但我不清楚如何创建上传和加载过程来处理我的服务器响应和请求。

谢谢。

【问题讨论】:

    标签: file-upload vue.js vuejs2 axios


    【解决方案1】:

    FilePond 作者在这里。

    我不完全确定我是否理解问题描述,但我会尽力提供帮助。我快速浏览了 Axios 文档 (https://github.com/axios/axios) 并设置了以下 sn-p。

    {
        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 也许你可以帮助我。看看这个:stackoverflow.com/questions/60219552/…
    • 如何将文件发送到服务器?使用文件池让我有点困惑。我应该设置 :server 属性还是 :process 属性?一般服务器如何创建放置文件?
    • @aliFalahati 将server 属性设置为服务器的 API 端点,您的服务器在该端点处处理包含文件发布的请求。您还可以设置一个函数,在这种情况下您需要设置一个自定义方法来将文件发布到您的服务器,请参阅:pqina.nl/filepond/docs/patterns/api/server
    猜你喜欢
    • 2019-06-28
    • 1970-01-01
    • 2020-04-09
    • 2022-10-14
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    相关资源
    最近更新 更多