1. 程式人生 > 其它 >使用Upload+Progress實現檔案上傳進度條實時更新功能

使用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 來源:簡書 著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。