JAVA配合Freemark模板生成word返回文件流,前台vue接收下载
作者:互联网
直接上vue的代码:
let sendData = {}
let successCallBack = (resp) => {
if (resp.status !== 200) {
this.$message.error('文件下载失败')
return false
}
// ********************* 接收文件流 ***********************
let blob = new Blob([resp.data], {
// type mimi类型 可以百度详细类型
// word文档为msword,application/pdfpdf文档为pdf application/vnd.ms-excel
// image/jpeg jpg
// type: `msword`
type: `application/msword`
})
let objectUrl = URL.createObjectURL(blob)
let link = document.createElement('a')
let fName = decodeURI(resp.headers['content-disposition'].split('=')[1])
link.href = objectUrl
link.setAttribute('download', fName)
document.body.appendChild(link)
link.click()
}
// this.$request.xxx() 是因为我将axios重新进行了封装
this.$Request.getFiles({
data: sendData,
responseType: 'blob', // **********这里的响应类型必须设置为'blob'
success: successCallBack,
requestDataType: 'form-data'
})
}
更详细的请参考:https://blog.csdn.net/CarryBest/article/details/102586653
标签:vue,word,resp,Freemark,application,link,blob,let,msword 来源: https://blog.csdn.net/qq_40652539/article/details/120126207