使用Upload+Progress實現文件上傳進度條實時更新功能


使用Upload+Progress實現文件上傳進度條實時更新功能,需要借助http-request屬性。具體使用方法如下:

  <el-upload
     action="#"
     :file-list="fileList"
     :on-change="changeData"
     :http-request="handleRequest"
     :before-upload="beforeUpload">
     <el-button class="btn upload-btn">上傳附件</el-button>
     <div slot="tip" class="el-upload__tip">上傳文件大小不超過50M</div>
  </el-upload>
  <el-progress :stroke-width="16" :percentage="progressPercent"></el-progress>

  
  //上傳前對文件大小進行校驗
  beforeUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 50;
      if (!isLt2M) {
        this.$message.error('上傳文件大小大小不能超過 50MB!');
        return isLt2M;
      }
  },
  changeData (file, fileList) {
       // 數據小於0.1M的時候按KB顯示
      const size = file.size/1024/1024 > 0.1 ? `(${(file.size/1024/1024).toFixed(1)}M)` : `(${(file.size/1024).toFixed(1)}KB)`
      file.name.indexOf('M')>-1 || file.name.indexOf('KB')>-1 ? file.name : file.name += size
  },

  handleRequest (data) {
      let formdata = new FormData()
      formdata.append('file', data.file)
      const config = {
        onUploadProgress: progressEvent => {
          // progressEvent.loaded:已上傳文件大小
          // progressEvent.total:被上傳文件的總大小
          this.progressPercent = Number((progressEvent.loaded / progressEvent.total * 100).toFixed(2))
        }
      }
      this.$axios.post(this.actionURL,formdata,config).then(res => {
        if (res.data.code===1) {}
      })
  },
4人點贊
ElementUI


作者:書山有路_勤為徑
鏈接:https://www.jianshu.com/p/3b00db2ace86
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM