Vue如何實現(xiàn)從response讀取流下載
更新時間:2025年04月11日 15:45:09 作者:自不惘
這篇文章主要介紹了Vue如何實現(xiàn)從response讀取流下載問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
Vue從response讀取流下載
1、創(chuàng)建公共方法
/** * 從response讀取流下載 * @param response 響應體 * @param suffix 文件后綴名 */ function downloadBlob(response,suffix){ let fileName = new Date().getTime()+"_"+suffix+'.xlsx' let blob = new Blob([response]);//response.data為后端傳的流文件 let url = window.URL.createObjectURL(blob); let downloadElement = document.createElement("a"); downloadElement.style.display = "none"; downloadElement.href = url; downloadElement.download = fileName; document.body.appendChild(downloadElement); downloadElement.click(); document.body.removeChild(downloadElement); window.URL.revokeObjectURL(url); } export default { downloadBlob }
2、掛載到Vue原型上
import blob from '@/utils/blob' Vue.prototype.$blob = blob
3、使用
/** 導出按鈕操作 */ handleExport() { this.$confirm('是否導出所有數(shù)據(jù)?', "警告", { confirmButtonText: "確定", cancelButtonText: "取消", type: "warning" }).then(function() { return exportData(); }).then(response => { this.$blob.downloadBlob(response,"數(shù)據(jù)表") }).catch(()=>{}) }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解為element-ui的Select和Cascader添加彈層底部操作按鈕
這篇文章主要介紹了詳解為element-ui的Select和Cascader添加彈層底部操作按鈕,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02vue3?內(nèi)容過多出現(xiàn)滾動條時滾動條自動定位到末端的操作代碼
這篇文章主要介紹了vue3?內(nèi)容過多出現(xiàn)滾動條時滾動條自動定位到末端的操作代碼,本文給大家介紹的非常詳細,需要的朋友參考下吧2024-05-05vue-cli的index.html中使用環(huán)境變量方式
這篇文章主要介紹了vue-cli的index.html中使用環(huán)境變量方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10