//导出doc文件
exportdoc() {
this.$http(
{
url: this.$http.adornUrl(
`/`
),
method: 'get',
params: {},
responseType: 'blob',
},
{ headers: 'application/x-download' }
).then(( data ) => {
// console.log(data);
// console.log(data.headers,'-----');
// console.log(data.headers.filename);
if (data.data.type === 'application/json') { //下载失败的判断
let reader = new FileReader()
reader.addEventListener('loadend', () => {
const message = JSON.parse(reader.result)
this.$message.error(message.msg || '下载失败')
})
reader.readAsText(data.data, 'utf-8')
} else {
let blob = new Blob([data.data], {
type:
'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8',
})
let downloadElement = document.createElement('a')
let href = window.URL.createObjectURL(blob) //创建下载的链接
downloadElement.href = href
downloadElement.download = decodeURIComponent(data.headers.filename) //下载后文件名需要先转码
document.body.appendChild(downloadElement)
downloadElement.click() //点击下载
document.body.removeChild(downloadElement) //下载完成移除元素
window.URL.revokeObjectURL(href) //释放掉blob对象
}
})
},
根据后端返回名字下载文件
最新推荐文章于 2023-11-09 10:34:09 发布