antdv 获取 axios文件上传实时进度
<template> <div style="max-width: 890px"> <a-form-model ref="ruleForm" :rules="rules" :model="form" :label-col="labelCol" :wrapper-col="wrapperCol" > <a-form-model-item ref="filefrom" label="名称" prop="name" > <a-input placeholder="name" v-model="form.name" /> </a-form-model-item> <a-form-model-item label="备注" prop="desc" > <a-input type="textarea" style="height: 60px;width: 100%" v-model="form.desc" /> </a-form-model-item> <a-form-model-item label="文件选择"> <a-upload :before-upload="beforeUpload" :remove="handleRemove" :multiple="false" :file-list="fileList" > <a-button> <a-icon type="upload" /> Select File </a-button> </a-upload> </a-form-model-item> <a-form-model-item :wrapper-col="{ span: 14, offset: 4 }"> <a-button type="primary" @click="onSubmit" >提交</a-button> <a-button style="margin-left: 10px;" @click="resetForm" >重填</a-button> </a-form-model-item> </a-form-model> <a-modal title="上传进度" v-model="visible" :footer="null" > <a-progress :percent="uploadRate" type="line" showInfo :strokeWidth="strokeWidth" :strokeColor="strokeColor" /> <a-progress :percent="uploadRate" type="dashboard" showInfo /> </a-modal> </div> </template>> <script> import { FileUpload } from '@/networks/file' export default { data () { return { visible: false, strokeWidth: 20, strokeColor: { '0%': '#108ee9', '100%': '#87d068' }, labelCol: { span: 4 }, wrapperCol: { span: 14 }, fileList: [], uploading: false, uploadRate: 0, form: { name: '', desc: '' }, rules: { name: [{ required: true, message: 'Please select filefrom!' }], } } }, methods: { // 文件移除 handleRemove (file) { const index = this.fileList.indexOf(file) const newFileList = this.fileList.slice() newFileList.splice(index, 1) this.fileList = newFileList }, beforeUpload (file) { this.fileList = [...this.fileList, file] return false }, // 上传文件点击确认 onSubmit () { this.visible = true this.$refs.ruleForm.validate(async valid => { if (valid) { const formData = new FormData() this.fileList.forEach((file) => { formData.append('file', file) }) for (const prop in this.form) { if (Object.prototype.hasOwnProperty.call(this.form, prop)) { formData.append(prop, this.form[prop]) } } this.uploading = true const config = { // headers: { 'Content-Type': 'multipart/form-data' }, onUploadProgress: e => { if (e.lengthComputable) { const rate = this.uploadRate = (e.loaded / e.total * 100 | 0) // 已上传的比例 if (rate < 1) { //这里的进度只能表明文件已经上传到后台,但是后台有没有处理完还不知道 //因此不能直接显示为100%,不然用户会误以为已经上传完毕,关掉浏览器的话就可能导致上传失败 //等响应回来时,再将进度设为100% this.uploadRate = rate } } } } const res = await FileUpload(formData, config) .then((res) => { if (res.code === 200) { this.fileList = [] this.uploading = false this.visible = false this.$msgsuccess(res.msg) this.$router.push({ name: 'FileList' }) // this.$emit('getPath',) } else { this.uploading = false this.$msgerror(res.msg) } }).catch(() => { return false }) } }) }, // 重置表单 resetForm () { this.$refs.ruleForm.resetFields() } } } </script> <style lang="sass" scoped> </style>
分类:
vue
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2020-06-29 kubeadm部署多master高可用kubernetes集群
2020-06-29 使用Operator部署Prometheus监控k8s集群