- js:
//下载
$("#xiaZaiExcel").click(function(){
//open() 方法用于打开一个新的浏览器窗口或查找一个已命名的窗口。
window.open("downloadExcelFile");
});
- controller:
- 使用ResponseEntity来实现返回图片或下载文件等操作
@RequestMapping("/downloadExcelFile")
public ResponseEntity<byte[]> download() throws IOException {
// 文件所在目录路径
String filePath = "C:/Users/xzx/Desktop/xxb.xls";
// 获取文件
File file = new File(filePath);
// 设置header
HttpHeaders headers = new HttpHeaders();
// application/octet‐stream二进制流数据(最常见的文件下载)
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
// //通知浏览器以attachment(下载方式) 下载文件,文件名称为指定名称
headers.setContentDispositionFormData("application/msexcel", "xxb.xls");
// 返回文件下载相关参数
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
}