当前位置:首页 » 《随便一记》 » 正文

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

5 人参与  2024年02月08日 16:21  分类 : 《随便一记》  评论

点击全文阅读


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://zhangshiyu.com/post/67841.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 林晚夏江肆年(进错房,嫁给八零最牛特种兵在线阅读)全文免费阅读无弹窗大结局_(林晚夏江肆年)进错房,嫁给八零最牛特种兵在线阅读免费阅读全文最新章节列表_笔趣阁(林晚夏江肆年) -
  • 进错房,嫁给八零最牛特种兵完整版阅读小说(林晚夏江肆年)全文免费阅读无弹窗大结局_(进错房,嫁给八零最牛特种兵完整版阅读)林晚夏江肆年免费阅读全文最新章节列表_笔趣阁(进错房,嫁给八零最牛特种兵完整版阅读) -
  • 新雪藏旧事全文全文(商云萝周砚京)全文免费阅读无弹窗大结局_(新雪藏旧事全文小说免费阅读)最新章节列表_笔趣阁(新雪藏旧事全文) -
  • 在线免费小说重生七零替嫁:不嫁教授,嫁军官_乔珊珊乔婉月新热门小说_热门小说乔珊珊乔婉月
  • 免费小说《冯云漪厉晋泽》已完结(冯云漪厉晋泽)热门小说大结局全文阅读笔趣阁
  • 祁兰湘邵黎晖小说_祁兰湘邵黎晖完整版大结局小说免费阅读
  • 完整免费小说老公心疼青梅将她留宿新房,却将怀孕的我赶出家门(乔玥傅慎行姜禾)_老公心疼青梅将她留宿新房,却将怀孕的我赶出家门(乔玥傅慎行姜禾)完本小说免费阅读(乔玥傅慎行姜禾)
  • 新雪藏旧事:结局+番外+完结免费小说在线阅读_小说完结推荐新雪藏旧事:结局+番外+完结商云萝周砚京热门小说
  • 初逢青山梦长安(顾怀瑾沈书妤)阅读 -
  • 无删减版《绝对权力:从天崩开局走上官途巅峰》在线免费阅读
  • 《绝对权力:从天崩开局走上官途巅峰》小说在线试读,《绝对权力:从天崩开局走上官途巅峰》最新章节目录
  • 裴泽苏星辰何娇(满目星辰不及你小说)精彩章节在线阅读

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1