这个方式存在浏览器兼容问题!!!
参考博文:vue2.x 下载后台传过来的流文件(excel)后乱码问题
变更下载文件名:
toExcelExport(){
this.$axios.post(url.paths.storageWarehouse.warehouseExcel,{params:this.$data.search},{responseType: 'arraybuffer'}).then((res) => {
let blob = new Blob([res], {type: "application/vnd.ms-excel"});
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = "仓库列表";
link.click();
});
} 但是这种方式只支持 chrome/firefox
整个实现的全部代码:
后台:
@RequestMapping(value = "/warehouseExcel", method = RequestMethod.POST)
@RequireToken
public void modelExcel(StorageWarehouse storageWarehouse, Integer excelType, HttpServletRequest request, HttpServletResponse response) {
OutputStream outputStream = null;
try {
HSSFWorkbook hssfWorkbook = createmodelExcel(storageWarehouse);
String fileName = "仓库列表" + System.currentTimeMillis();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="
+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
outputStream = response.getOutputStream();
hssfWorkbook.write(outputStream);
outputStream.flush();
} catch (Exception e) {
logger.error(e.getMessage());
}finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
}
}
}
} 前台:
toExcelExport(){
this.$axios.post(url.paths.storageWarehouse.warehouseExcel,{params:this.$data.search},{responseType: 'arraybuffer'}).then((res) => {
let blob = new Blob([res], {type: "application/vnd.ms-excel"});
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = "仓库列表";
link.click();
});
},
本文介绍了在Vue2.x中遇到的流文件下载时出现的乱码问题,以及如何解决浏览器兼容性问题。同时,文章详细讲解了如何变更下载的Excel文件名称,提供了后台处理文件流的代码示例。
926





