前端实现txt文件下载-后端传文件流,前端下载
前端实现txt文件下载-后端传文件流,前端下载
1 | <a class = "abAccount" @click= "downLoadTxt(scope.row.taskId, scope.row.taskName)" >采集异常账号下载</a> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | // 文件下载 downLoadTxt(taskId, taskName) { let _this = this ; this .$axios({ withCredentials: false , Accept: "application/json, text/plain, */*" , url: this .taskNewUrl + "admin/taskManage/downLoad" , // responseType: "blob", method: "post" , data: { data: { taskId: taskId, }, }, }) .then( function (res) { const { data } = res; const blob = new Blob([data]); // let disposition = decodeURI(res.headers["content-disposition"]); // 从响应头中获取文件名称 let fileName = taskName; if ( "download" in document.createElement( "a" )) { // 非IE下载 const elink = document.createElement( "a" ); elink.download = fileName; elink.style.display = "none" ; elink.href = URL.createObjectURL(blob); document.body.appendChild(elink); elink.click(); URL.revokeObjectURL(elink.href); // 释放URL 对象 document.body.removeChild(elink); } else { // IE10+下载 navigator.msSaveBlob(blob, fileName); } }) . catch ((err) => { console.log(err); }); }, |
浏览器请求成功效果
来源:https://www.cnblogs.com/tdcqcrtd/p/14789957.html