前端GET/POST請(qǐng)求下載文件多種方式代碼示例
下載post
export const download = (data, projectId) => { return request({ url: BaseUrl + '/xxx/xxx/xxx', headers: { "Project-Id": projectId, httpWhite: true, }, responseType: "blob",//文件流 method: 'post', data }) }
<el-button size="small" type="primary" class="downLoadTemplate" @click="downloadFile(row)"> <i class="iconfont yun-xiazai"></i> <span class="first">下載數(shù)據(jù)模板</span> </el-button> //點(diǎn)擊下載 const downloadFile(row){ const params = { 需要傳遞的參數(shù):'xxxx', id:row.id, //示例, } download(params, this.projectIds).then((res) => { if (res.status === 200) { this.downloadDataTemplate(res); } }); } //下載數(shù)據(jù)模板 downloadDataTemplate(res) { if (!res.data) { return; } const fileName = decodeURIComponent( res.headers["content-disposition"].split("filename=")[1] ); const blob = new Blob([res.data], { type: "application/vnd.openxmlformats- officedocument.spreadsheetml.sheet;charset=utf-8", }); const downloadElement = document.createElement("a"); const href = window.URL.createObjectURL(blob); // 創(chuàng)建下載的鏈接 downloadElement.href = href; // 文件名中有中文 則對(duì)文件名進(jìn)行轉(zhuǎn)碼 downloadElement.download = fileName; // 下載后文件名 document.body.appendChild(downloadElement); downloadElement.click(); // 點(diǎn)擊下載 document.body.removeChild(downloadElement); // 下載完成移除元素 window.URL.revokeObjectURL(href); // 釋放blob對(duì)象 },
Get下載方法
通用get下載方法 99%可以使用
const downError = `BaseUrl+/xxx/xxx/xxxx?${this.tokenHeader}=${getToken()}&projectId=${this.projectId}&spaceId=${this.spaceId}`; window.open(downError, "_self");//調(diào)用window方法
特殊get下載方法 1%才能用到 一般用不到 (僅用于個(gè)人記錄)
這種使用于需要在hearder里面添加projecrt-Id等參數(shù) 一般的都不需要 主要看后端接口能不能使用
使用下面這種方法 最主要get下載的請(qǐng)求 是responseType:blob 文件流
export const exportPersonnel = (params) => request({ url: BaseUrl + '/exportxxx/xxxx', headers: { "Project-Id": params.projectId, }, method: 'get', responseType: 'blob', //文件流方法 params, })
// 導(dǎo)出 exportcustomer() { let downStr = { ...this.params }; exportPersonnel(downStr).then((r) => { if (r.status === 200) { //獲取下載文件名 const fileName = decodeURIComponent( r.headers["content-disposition"].split("filename=")[1] ); // 創(chuàng)建 a 元素 const link = document.createElement('a'); const href = window.URL.createObjectURL(r.data)//創(chuàng)建下載鏈接 link.href = href;// 設(shè)置下載鏈接的 URL link.style.display = "none"; link.download = fileName; // 下載后文件名 document.body.appendChild(link); link.click(); // 點(diǎn)擊下載 document.body.removeChild(link); // 下載完成移除元素 window.URL.revokeObjectURL(href); // 釋放blob對(duì)象 } }); },
總結(jié)
到此這篇關(guān)于前端GET/POST請(qǐng)求下載文件多種方式的文章就介紹到這了,更多相關(guān)前端GET/POST請(qǐng)求下載文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談html轉(zhuǎn)義及防止javascript注入攻擊的方法
下面小編就為大家?guī)?lái)一篇淺談html轉(zhuǎn)義及防止javascript注入攻擊的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12學(xué)習(xí)使用bootstrap的modal和carousel
這篇文章主要教大家學(xué)會(huì)用bootstrap的modal和carousel,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12JavaScript實(shí)現(xiàn)簡(jiǎn)單拖拽效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單拖拽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09JS判斷鍵盤是否按的回車鍵并觸發(fā)指定按鈕點(diǎn)擊操作的方法
下面小編就為大家?guī)?lái)一篇JS判斷鍵盤是否按的回車鍵并觸發(fā)指定按鈕點(diǎn)擊操作的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02angular bootstrap timepicker TypeError提示怎么辦
這篇文章主要介紹了angular bootstrap timepicker TypeError提示的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06JS和Canvas實(shí)現(xiàn)圖片的預(yù)覽壓縮和上傳功能
這篇文章主要介紹了JS和Canvas實(shí)現(xiàn)圖片的預(yù)覽壓縮和上傳功能,實(shí)現(xiàn)此功能大概有兩步,第一步用戶選擇需要上傳的圖片,第二步獲取圖片資源壓縮預(yù)覽上傳,具體實(shí)現(xiàn)代碼大家參考下本文2018-03-03