附件下载:
后台接口返回文件流:
前端调取后台API接口:一定要注意写上: responseType: “blob”,否则下载后内容为乱码
export function downloadFile(query) {
return request({
url: "/assess/file/download",
method: "get",
params: query,
**responseType: "blob"**
});
}
```在vue 页面中引入api ,接口处理部分:
// 后台返回文件流转为blob,创建a标签,去下载
getdoen (index) {
let urls = {
url: index,
}
downloadFile(urls).then(response => {
const fileName =index
const bolb = new Blob([response], { type: '' })
if ('download' in document.createElement('a')) {
const link = document.createElement('a')
link.download = fileName
link.style.display = 'none'
link.href = URL.createObjectURL(bolb)
document.body.appendChild(link)
link.click()
URL.revokeObjectURL(link.href)
document.body.removeChild(link)
}
});
},