前端请求后接收处理
Blob,实现内容保存为压缩包格式
var blob = new Blob([res.data], { type: 'application/x-tar' }) //tar包,如果是zip,则为application/x-zip-compressed
let linkNode = document.createElement('a')
linkNode.download = fileName + '.tar'
linkNode.style.display = 'none'
linkNode.href = URL.createObjectURL(blob)
document.body.appendChild(linkNode)
linkNode.click()
URL.revokeObjectURL(linkNode.href)
document.body.removeChild(linkNode)
完整代码
downFileTimeOut(this.url.exportTaskObjectData, record,5*60*1000).then((data)=>{
if (!data) {
this.$message.warning("文件下载失败")
return
}
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(new Blob([data],{type: 'application/x-tar'}), fileName+'.zip')
}else{
let url = window.URL.createObjectURL(new Blob([data],{type: 'application/x-tar'}))
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', fileName+'.zip')
document.body.appendChild(link)
link.click()
document.body.removeChild(link); //下载完成移除元素
window.URL.revokeObjectURL(url); //释放掉blob对象
}
this.$message.warning("文件下载成功")
}).finally(() => {
this.loading = false;
});
workbook后端处理
//保存多workbook集合,也是zip中的excel文件
List<Workbook> hssfWorkbookList = new ArrayList<>();
//每个excel文件的名称
List<String> xlsNameList = new ArrayList<>();
//设置响应头
response.setHeader("content-disposition", "attachment;filename=" + task.getName() + ".zip");
response.setContentType("application/x-zip-compressed");
//输出流
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
//压缩流
ZipOutputStream zipOut = new ZipOutputStream(toClient);
//list表示该zip中有多少个excel
for (TbZsTaskObject item:list
) {
//通过数据生成workbook (这里就是核心,把需要压缩的文件放到集合中)
Workbook workbook = tbZsTaskObjectService.getAllTableWorkbook(item.getId());
//保存名称
xlsNameList.add(item.getSchoolname()+"_"+item.getTaskSchoolcode());
//保存workbook
hssfWorkbookList.add(workbook);
}
//遍历excel集合压缩文件zip 这里就是压缩文件了
for (int i = 0; i < hssfWorkbookList.size(); i++) {
ZipEntry entry = new ZipEntry(xlsNameList.get(i) + ".xlsx");
zipOut.putNextEntry(entry);
hssfWorkbookList.get(i).write(zipOut);
}
//把输出流响应到浏览器
zipOut.flush();
//关闭流
zipOut.close();
file文件处理
//保存多workbook集合,也是zip中的excel文件
List<String> hssfWorkbookList = new ArrayList<>();
//每个excel文件的名称
List<String> xlsNameList = new ArrayList<>();
//设置响应头
response.setHeader("content-disposition", "attachment;filename=" + "aaaaa" + ".zip");
response.setContentType("application/x-zip-compressed");
//输出流
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
//压缩流
ZipOutputStream zipOut = new ZipOutputStream(toClient);
String[] paths= {"aaa","bb"};
for (String path: paths) {
String filePath = uploadpath + File.separator + path;
//保存名称
xlsNameList.add(path));
//保存文件
hssfWorkbookList.add(filePath);
}
//遍历excel集合压缩文件zip
for (int i = 0; i < hssfWorkbookList.size(); i++) {
// 创建一个ZipEntry
zipOut.putNextEntry(new ZipEntry(xlsNameList.get(i) + ".docx"));
String filePath = hssfWorkbookList.get(i);
// 将原文件的字节内容,写入zip压缩包
zipOut.write(Files.readAllBytes(new File(filePath).toPath()));
}
//把输出流响应到浏览器
zipOut.flush();
//关闭流
zipOut.close();