【问题标题】:Progress Bar with axiosaxios 的进度条
【发布时间】:2023-03-29 17:43:01
【问题描述】:

我必须使用进度条显示文件的上传状态。我正在使用 axios 发出 http 请求。我从他们的github页面https://github.com/mzabriskie/axios/blob/master/examples/upload/index.html

我的代码如下所示:

this.store().then(() => {
    var form = new FormData();
        form.append('video', this.file);
        form.append('uid', this.uid);

        axios.post('/upload', form, {
            progress: (progressEvent) => {
                    if (progressEvent.lengthComputable) {
                       console.log(progressEvent.loaded + ' ' + progressEvent.total);
                       this.updateProgressBarValue(progressEvent);
                    }
           }
       })                   
});

但是,它根本没有执行console.log(progressEvent.loaded + ' ' + progressEvent.total);,也没有调用this.updateProgressBarValue(progressEvent);

我该如何解决这个问题??

【问题讨论】:

    标签: javascript progress-bar vuejs2 axios


    【解决方案1】:

    我认为问题出在“进度”事件本身,正如您可以在Axios configuration 中看到的那样,不支持进度本身。相反,您应该听 onUploadProgressonDownloadProgress

    另一个问题是获取总长度,我尝试通过以下方式获取:查看 lengthComputable,如果不尝试从标头获取长度,如果不尝试获取解压缩的内容长度(作为最后的手段),那么你应该是能够用价值做任何你想做的事情。

    这不是一个万无一失的实现!只要总长度不可用,它就会失败。

    为了使它更可靠,您可以使用 setInterval 实现“假”进度,以每秒手动增加进度。承诺解决后,手动将进度设置为 100%。如果你使用 CSS 过渡来实现它,你应该得到一个平滑的解决方案,即使进度并不总是“正确”

    如果您需要更多代码,我制作了一个类似的加载程序 (GitHub link)。

                    onUploadProgress: (progressEvent) => {
                    const totalLength = progressEvent.lengthComputable ? progressEvent.total : progressEvent.target.getResponseHeader('content-length') || progressEvent.target.getResponseHeader('x-decompressed-content-length');
                    console.log("onUploadProgress", totalLength);
                    if (totalLength !== null) {
                        this.updateProgressBarValue(Math.round( (progressEvent.loaded * 100) / totalLength ));
                    }
                });
    

    【讨论】:

    【解决方案2】:

    我找到了答案。活动名称是onUploadProgress,我使用的是progress

    【讨论】:

      【解决方案3】:

      这是一个非常方便的库,无需太多编码即可实现这一目标 - https://github.com/rikmms/progress-bar-4-axios/

      【讨论】:

      • 该库的问题是似乎没有停止加载栏的功能
      【解决方案4】:

      之前,我使用 onUploadProgress 事件制作进度条。

      这是我的示例代码,如下所示。

      import React from 'react';
      import Progress from 'react-progressbar';
      import axios from 'axios';
      export default class App extends React.Component {
        constructor(props) {
          super(props);
          this.state = {
            progress: 0,
            uploadFile: null,
          }
        }
        onChangeFile = (event) => {
          this.setState({ uploadFile: event.target.files[0] })
        }
      
        onUpload = () => {
          const config = {
            onUploadProgress: progressEvent => {
              let { progress } = this.state;
              progress = (progressEvent.loaded / progressEvent.total) * 100;
              this.setState({ progress });
            }
          }
      
          let formData = new FormData();
          formData.append("file", this.state.uploadFile);
          axios.post('http://your_backend_url/api/v1/upload', formData, config).then(res => {
            if (res.data.status == 200) {
              console.log("done: ", res.data.message);
            }
          }).catch(err => {
            console.log("error: ", err.message);
          })
        }
      
        render() {
          return (
            <div className="App">
              <Progress completed={this.state.progress} />
              <input type="file" onChange={this.onChangeFile} />
              <button onClick={this.onUpload.bind(this)} >File Upload</button>
            </div>
          )
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-12-10
        • 2020-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-13
        • 2020-01-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多