web前端开发

Axios的config如何配置

2017-10-27  本文已影响0人  一个废人

https://github.com/axios/axios#request-config

 // `onUploadProgress` allows handling of progress events for uploads
  onUploadProgress: function (progressEvent) {
    // Do whatever you want with the native progress event
  },

工作中用到的是利用一个callback函数拿到上传时的进度.

FileUploader.vue

       let callback = (progress)=>{
          self.progress = progress
        }
        upload(formData,callback).then((response)=>{
          console.log(response)
        }).catch((err)=>{
        })

api.js

export function upload(file,callback) {
  const url = `${BASE_URL}/api/upload`;
  let config = {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    onUploadProgress: function(progressEvent) {
      let percentCompleted = Math.round( (progressEvent.loaded * 100) / progressEvent.total );
      callback(percentCompleted)
    }
  }
  return axios.post(url,file,config);
}
上一篇下一篇

猜你喜欢

热点阅读