前端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)文章
JavaScript基于面向?qū)ο髮?shí)現(xiàn)的猜拳游戲
這篇文章主要介紹了JavaScript基于面向?qū)ο髮?shí)現(xiàn)的猜拳游戲,結(jié)合完整實(shí)例形式分析了javascript基于面向?qū)ο髮?shí)現(xiàn)猜拳游戲的具體頁(yè)面布局、樣式及功能相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
bootstrap table實(shí)現(xiàn)x-editable的行單元格編輯及解決數(shù)據(jù)Empty和支持多樣式問(wèn)題
本文著重解決x-editable編輯的數(shù)據(jù)動(dòng)態(tài)添加和顯示數(shù)據(jù)為Empty的問(wèn)題,還有給表格單元格的內(nèi)容設(shè)置多樣式,使得顯示多樣化,需要的朋友可以參考下2017-08-08
前端項(xiàng)目npm?install?安裝依賴(lài)報(bào)錯(cuò)的解決方案(三種問(wèn)題解決方案)
本文給大家介紹前端項(xiàng)目npm?install?安裝依賴(lài)報(bào)錯(cuò)的解決方案(三種問(wèn)題解決方案),給大家總結(jié)了前端項(xiàng)目安裝依賴(lài),遇到過(guò)的問(wèn)題,每一種問(wèn)題給大家完美解決方案,感興趣的朋友一起看看吧2023-12-12
js實(shí)現(xiàn)Select下拉框具有輸入功能的方法
這篇文章主要介紹了js實(shí)現(xiàn)Select下拉框具有輸入功能的方法,實(shí)例分析了兩種比較常見(jiàn)的實(shí)現(xiàn)方法,是非常實(shí)用的技巧,需要的朋友可以參考下2015-02-02
javascript中DOM復(fù)選框選擇用法實(shí)例
這篇文章主要介紹了javascript中DOM復(fù)選框選擇用法,實(shí)例分析了javascript操作復(fù)選框?qū)崿F(xiàn)全選與反選的相關(guān)技巧,需要的朋友可以參考下2015-05-05
用ajax實(shí)現(xiàn)的自動(dòng)投票的代碼
用ajax實(shí)現(xiàn)的自動(dòng)投票的代碼...2007-03-03
JavaScript正則表達(dá)式校驗(yàn)與遞歸函數(shù)實(shí)際應(yīng)用實(shí)例解析
這篇文章主要介紹了JavaScript正則表達(dá)式校驗(yàn)與遞歸函數(shù)實(shí)際應(yīng)用,需要的朋友可以參考下2017-08-08

