后端处理代码
String fileName = id + "-xxxx.docx";
try {
// 模板存放地址
String templateUrl = uploadpath + File.separator + "template" + File.separator + "clue_down_template.docx";
ModelAndView mv = new ModelAndView(new JeecgTemplateWordView());
mv.addObject(TemplateWordConstants.FILE_NAME, fileName);
mv.addObject(TemplateWordConstants.URL, templateUrl);
// 添加模板参数
mv.addObject(TemplateWordConstants.MAP_DATA, getClueDownMap(id)); //data
return mv;
} catch (Exception e) {
log.error("下载xxxxxxxxxx表异常--》{}", e);
return null;
}
前端处理modelAndView
downFile(this.url.downLoad + '/' + id).then((res) => {
if (!res) {
this.$message.warning('文件下载失败')
return
}
let blob = new Blob([res], { type: 'application/msword' })
let downloadElement = document.createElement('a')
let href = window.URL.createObjectURL(blob) // 创建下载的链接
downloadElement.href = href
downloadElement.download = 'xxxxxx.docx' // 下载后文件名
document.body.appendChild(downloadElement)
downloadElement.click() // 点击下载
document.body.removeChild(downloadElement) // 下载完成移除元素
window.URL.revokeObjectURL(href) // 释放掉blob对象
})
},