新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
下載文件流文件名轉(zhuǎn)碼-創(chuàng)新互聯(lián)
下載文件,后端返回文件流,前端下載并讀取后端返回文件名content-disposition
后端接口返回content-disposition:
前端讀取文件名并下載
1.axios返回請求響應全部數(shù)據(jù)
service.interceptors.response.use((response: AxiosResponse) =>{
const result = response.data;
const { config: { responseType } } = response;
if (responseType === 'blob') {
return response;
}
2.接口調(diào)用返回中讀取解碼:decodeURI(fileName)
getExport().then((res:any) =>{
const fileName = res.headers['content-disposition'].split(';')[1].split('filename=')[1];
// 轉(zhuǎn)換編譯content-disposition的文件名:decodeURI
downloadFile(decodeURI(fileName), res.data);
});
3.文檔流下載方法
export function downloadFile(fileName:string, content:Blob, unicode = 'application/octet-stream;charset=utf-8'):void {
// const blob = new Blob([content], { type: unicode });
if ('download' in document.createElement('a')) { // 非IE下載
const downloadElement = document.createElement('a');
let href = '';
if (window.URL) {
href = window.URL.createObjectURL(content);
} else {
href = window.webkitURL.createObjectURL(content);
}
downloadElement.href = href;
downloadElement.download = fileName;
document.body.appendChild(downloadElement);
downloadElement.click();
if (window.URL) {
window.URL.revokeObjectURL(href);
} else {
window.webkitURL.revokeObjectURL(href);
}
document.body.removeChild(downloadElement);
}
你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
分享文章:下載文件流文件名轉(zhuǎn)碼-創(chuàng)新互聯(lián)
文章位置:http://fisionsoft.com.cn/article/geijd.html