前后端分離下載后端返回的數(shù)據(jù),文件類型為excel文件:
實(shí)現(xiàn)下載的函數(shù),后臺(tái)數(shù)據(jù)返回的類型是bufferArray
?downLoad(filename,?content)?{//filename 文件名,content 下載的內(nèi)容
??????var?aLink?=?document.createElement("a");
??????var?blob?=?new?Blob([content],?{
????????type:
??????????"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" //文件類型
??????});
??????var?evt?=?new?Event("click");
??????aLink.download?=?filename;
??????aLink.href?=?URL.createObjectURL(blob);
??????aLink.click();
??????URL.revokeObjectURL(blob);
????}
在獲取后臺(tái)接口成功的函數(shù)中調(diào)用下載函數(shù)(vue)
API.download({?id:?id?})
????????.then(res?=>?{
??????????this.downLoad("code.xlsx",?new?Uint8Array(res.data).buffer);
????????})
????????.catch(err?=>?{});