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

【vue2】前端实现下载后端返回的application/octet-stream文件流

1、下载csv/txt时
此时无须修改接口的响应格式

let filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
let matches = filenameRegex.exec(data.headers['content-disposition']);
let blob = new Blob(['\uFEFF' + data.data], {//目前只有csv格式type: 'text/csv;charset=utf-8'
})
var downloadElement = document.createElement('a');
var href = window.URL.createObjectURL(blob);
downloadElement.href = href;
downloadElement.download = matches[1].substring(0, matches[1].lastIndexOf('.'));
document.body.appendChild(downloadElement); 
downloadElement.click(); //注销掉
window.URL.revokeObjectURL(href); //清除生成的链接,会占用一些东西,不知道啥,反正运行慢点

2、下载Excel时,需要修改一下接口的响应格式为blob

axios({method: "POST",url: constants.url.QUERY_DESCRIBE_TEST_REPORT_DOWNLOAD,responseType: "blob",//FIXME 很重要timeout: 60000,data: {TestTaskId,},}).then((response) => {console.log("response", response);if (!response || !response.data) {self.$message({type: "error",message: "导出失败!",});return;}let filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;let matches = filenameRegex.exec(response.headers["content-disposition"]);let blob = new Blob([response.data]);let downloadFilename = matches[1];if (window.navigator && window.navigator.msSaveOrOpenBlob) {//兼容ie浏览器window.navigator.msSaveOrOpenBlob(blob, downloadFilename);} else {//谷歌,火狐等浏览器let url = window.URL.createObjectURL(blob);let downloadElement = document.createElement("a");downloadElement.style.display = "none";downloadElement.href = url;downloadElement.download = downloadFilename;document.body.appendChild(downloadElement);downloadElement.click();document.body.removeChild(downloadElement);window.URL.revokeObjectURL(url);}}).catch((err) => {this.$message.error(err);});

{".3gp",    "video/3gpp"},
{".apk",    "application/vnd.android.package-archive"},
{".asf",    "video/x-ms-asf"},
{".avi",    "video/x-msvideo"},
{".bin",    "application/octet-stream"},
{".bmp",    "image/bmp"},
{".c",      "text/plain"},
{".csv",    "text/csv;charset=utf-8"},
{".class",  "application/octet-stream"},
{".conf",   "text/plain"},
{".cpp",    "text/plain"},
{".doc",    "application/msword"},
{".docx",   "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
{".xls",    "application/vnd.ms-excel"},
{".xlsx",   "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
{".exe",    "application/octet-stream"},
{".gif",    "image/gif"},
{".gtar",   "application/x-gtar"},
{".gz",     "application/x-gzip"},
{".h",      "text/plain"},
{".htm",    "text/html"},
{".html",   "text/html"},
{".jar",    "application/java-archive"},
{".java",   "text/plain"},
{".jpeg",   "image/jpeg"},
{".jpg",    "image/jpeg"},
{".js",     "application/x-javascript"},
{".log",    "text/plain"},
{".m3u",    "audio/x-mpegurl"},
{".m4a",    "audio/mp4a-latm"},
{".m4b",    "audio/mp4a-latm"},
{".m4p",    "audio/mp4a-latm"},
{".m4u",    "video/vnd.mpegurl"},
{".m4v",    "video/x-m4v"},
{".mov",    "video/quicktime"},
{".mp2",    "audio/x-mpeg"},
{".mp3",    "audio/x-mpeg"},
{".mp4",    "video/mp4"},
{".mpc",    "application/vnd.mpohun.certificate"},
{".mpe",    "video/mpeg"},
{".mpeg",   "video/mpeg"},
{".mpg",    "video/mpeg"},
{".mpg4",   "video/mp4"},
{".mpga",   "audio/mpeg"},
{".msg",    "application/vnd.ms-outlook"},
{".ogg",    "audio/ogg"},
{".pdf",    "application/pdf"},
{".png",    "image/png"},
{".pps",    "application/vnd.ms-powerpoint"},
{".ppt",    "application/vnd.ms-powerpoint"},
{".pptx",   "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
{".prop",   "text/plain"},
{".rc",     "text/plain"},
{".rmvb",   "audio/x-pn-realaudio"},
{".rtf",    "application/rtf"},
{".sh",     "text/plain"},
{".tar",    "application/x-tar"},
{".tgz",    "application/x-compressed"},
{".txt",    "text/plain"},
{".wav",    "audio/x-wav"},
{".wma",    "audio/x-ms-wma"},
{".wmv",    "audio/x-ms-wmv"},
{".wps",    "application/vnd.ms-works"},
{".xml",    "text/plain"},
{".z",      "application/x-compress"},
{".zip",    "application/x-zip-compressed"},
{"",        "*/*"}
http://www.lryc.cn/news/138473.html

相关文章:

  • 【Java】SM2Utils(国密 SM2 工具类)
  • 『C语言入门』初识C语言
  • jira创建条目rest实用脚本
  • 红外/可见光图像配准融合
  • 更高效稳定 | 基于ACM32 MCU的编程直流电源应用方案
  • postgresql创建一个只读账户指定数据库
  • CSDN编程题-每日一练(2023-08-25)
  • 前端面试:【前端工程化】构建工具Webpack、Parcel和Rollup
  • 大型企业是否有必要进行数字化转型?
  • 05有监督学习——神经网络
  • JavaWeb_LeadNews_Day7-ElasticSearch, Mongodb
  • redux中间件理解,常见的中间件,实现原理。
  • 麒麟系统上安装 MySQL 8.0.24
  • vue 展开和收起
  • 限制立方样条(RCS)中的P for overall和P for nonlinear的计算
  • vue3+ts引入echarts并实现自动缩放
  • Compressor For Mac强大视频编辑工具 v4.6.5中文版
  • maven工程的目录结构
  • 5.1 webrtc线程模型
  • 【Linux网络】Cookie和session的关系
  • android 硬编码保存mp4
  • gitlab合并分支
  • 手撕 `np.transpose` : 三维数组的循环转置
  • 计算机竞赛 基于Django与深度学习的股票预测系统
  • CSS 小技能(一):HTML 两个图片竖着平铺、设置图片点击、设置滚动条颜色
  • 【论文阅读】CONAN:一种实用的、高精度、高效的APT实时检测系统(TDSC-2020)
  • P1328 [NOIP2014 提高组] 生活大爆炸版石头剪刀布
  • 基于Android水果蔬菜果蔬到家商城系统 微信小程序uniAPP的开发与实现
  • 【Python】从入门到上头—Python基础(2)
  • leetcode刷题之283:移动零