当前位置: 首页 > news >正文

2013 ~【VUE+ ElementUI】——【上传、下载】进度计算

【VUE+ ElementUI】——【上传、下载】进度计算

  • 上传:FormData方式上传,监听 onUploadProgress
  • 下载:blob文件流下载,监听 onDownloadProgress

上传:FormData方式上传,监听 onUploadProgress

<el-upload:disabled="isUploading"ref="upload":limit="1"accept=".bin":headers="headers":action="url":show-file-list="false":http-request="uploadSectionFile"class="uploadStyle"><el-button:loading="isUploading":disabled="isUploading"slot="trigger"type="primary"plainsize="small"icon="el-icon-upload2">{{ isUploading ? "文件上传中" : '上传'}}</el-button><el-progress v-if="isShow" class="poFix" :percentage="uploadProgress" :text-inside="false" :color="customColors" :stroke-width="4" /></el-upload>
// 接口:上传文件 form-data
export function uploadFile(data, config) {return request({url: '/moduleVersion/saveVersion',method: 'post',data: data,headers: {'Content-Type': 'multipart/form-data'  // 以表单传数据的格式来传递fromdata},timeout: 2 * 60 * 1000, // 超时时长设为2分钟...config})
}
/** 自定义上传 */uploadSectionFile(data) {this.uploadProgress = 0;const file = data.file;// const isLt2M = file.size / 1024 / 1024 < 500;// if (!isLt2M) {//   this.$message.error("文件大小不得超过500MB");//   return;// }this.$refs.upload.clearFiles(); // 这样才能二次选择其它文件this.isShow = true;this.submitForm(file);},
/** 上传 提交按钮 */
submitForm: function (file) {let that = this;let formData = new FormData();formData.append("file", file);const config = {onUploadProgress: progressEvent => {if (progressEvent.lengthComputable) {that.uploadProgress = Math.round((progressEvent.loaded / progressEvent.total) * 100);}}};uploadFile(formData, config).then((response) => {that.isUploading = false;that.$modal.msgSuccess("上传成功");}).catch(() => {that.isUploading = false;});
}

下载:blob文件流下载,监听 onDownloadProgress

// 接口:下载文件 blob
export function downloadFile(data, config) {return request({url: '/syslog/download',method: 'post',data: data,responseType: 'blob', // 接收blob文件流格式timeout: 2 * 60 * 1000, // 设置超时2分钟...config})
}
/** 导出模板操作 */handleExport(row) {let that = this;that.downloadProgress = 0;let downloadLoadingInstance = Loading.service({ text: "正在下载数据,请稍候 " + that.downloadProgress + '%', spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", })const config = {onDownloadProgress: progressEvent => {if (progressEvent.lengthComputable) {that.downloadProgress = Math.round((progressEvent.loaded / progressEvent.total) * 100);downloadLoadingInstance.text = "正在下载数据,请稍候 " + that.downloadProgress + '%';}}};// 从后端请求到 文件流数据const fullPath = row.fullPath;downloadLog(fullPath, config).then((response) => {let downloadName = `${this.parseTime(new Date().getTime())}系统日志.zip`;this.downloadBlob(response, downloadName);downloadLoadingInstance.close(); // 关闭加载loading效果}).catch(() => {downloadLoadingInstance.close(); // 关闭加载loading效果);},// 下载文件流格式的文件downloadBlob(response, downloadName) {let blob = new Blob([response], {type: "application/json;charset=utf-8",});let href = window.URL.createObjectURL(blob); // 创建下载的链接if (window.navigator.msSaveBlob) {try {window.navigator.msSaveBlob(blob, downloadName);} catch (e) {console.log(e);}} else {// 谷歌浏览器 创建a标签 添加download属性下载let downloadElement = document.createElement("a");downloadElement.href = href;downloadElement.target = "_blank";downloadElement.download = downloadName; // 下载后文件名document.body.appendChild(downloadElement);downloadElement.click(); // 点击下载document.body.removeChild(downloadElement); // 下载完成移除元素window.URL.revokeObjectURL(href); // 释放掉blob对象}}
http://www.lryc.cn/news/169623.html

相关文章:

  • android可见即可说实现方案
  • Pikachu Burte Force(暴力破解)
  • SpringMVC之JSON返回及异常处理
  • SkyWalking快速上手(六)——告警
  • docker run:--privileged=true选项解析(特权模式:赋予容器几乎与主机相同的权限)
  • 计算机专业毕业设计项目推荐06-工作室管理系统(Java+Vue+Mysql)
  • Python 文件的读写操作
  • 多线程回顾、集合Collection、Set、List等基本知识
  • 分享5款用起来很好用的软件,总有一款适合你
  • 大数据学习1.5-单机Hadoop
  • Cesium对实体元素鼠标点击popup div信息框
  • 有多条业务线,mysql建多库多表比较好还是一个库多个表比较好呢?
  • C++---异常处理
  • 接口自动化测试(Python+Requests+Unittest)
  • 驱动开发,IO多路复用(select,poll,epoll三种实现方式的比较)
  • 大数据-玩转数据-oracel字符串分割转化为多列
  • GCP设置Proxy来连接Cloud SQL
  • Python:为何成为当下最热门的编程语言?
  • 【echarts入门】:vue项目中应用echarts
  • Seata 源码篇之AT模式启动流程 - 上 - 02
  • FFMPEG视频压缩与Python使用方法
  • SpringMVC自定义注解---[详细介绍]
  • 5.4 转换数据
  • 雷池社区WAF:保护您的网站免受黑客攻击 | 开源日报 0918
  • 链表反转-LeetCode206
  • 北邮22级信通院数电:Verilog-FPGA(3)实验“跑通第一个例程”modelsim仿真及遇到的问题汇总(持续更新中)
  • 4G工业路由器,开启智能工厂,这就是关键所在
  • 计组-机器字长、存储字长、指令字长以及和他们有关的机器位数
  • 解决express服务器接收post请求报错:“req.body==> undefined“
  • 5.zigbee的开发,串口putchar重定向(使用print),单播实验,usb抓包实验