我试图从服务器返回一个文件作为响应,以便从客户端下载。
我不确定我做错了什么我按照一些例子但我仍然无法下载文件,请假设该文件已存储在目录中。
这是我的代码:
@RequestMapping(value="/reports/downloadCsv", method=RequestMethod.POST)
public void saveToCsv(
@Validated HttpServletRequest request,
@Validated HttpSession session,
@Validated HttpServletResponse response,
@RequestParam String report) {
InputStream in = new FileInputStream("C:\\server\\reports\\AccessReport.csv");
response.setContentType("text/csv;charset=utf-8");
response.setHeader("Content-disposition", "attachment; filename=AccessReport.csv");
FileCopyUtils.copy(in, response.getOutputStream());
response.flushBuffer();
}
这是我的ajax电话:
$.ajax({
url: "/server/reports/downloadCsv",
type: "post",
data: {
report: "AccessReport.csv"
},
type: 'POST',
success: function(res){
}
});