vue+element-ui中上传文件使用Progress自定义实时更新进度条
使用Upload+Progress实现文件上传进度条实时更新功能,需要借助http-request属性。具体使用方法如下:<el-uploadaction="#":file-list="fileList":on-change="changeData":http-request="handleRequest":before-up...
·
使用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) {}
})
},
更多推荐
已为社区贡献3条内容
所有评论(0)