场景描述:文件下载,调用后台请求,一般直接用form 表单,action 对应上后台控制器的路径就可以。但是这种方式不能够获得后台传回来的值,比如,想要下载的文件不存在,或者是遇到啥意外了导致下载失败,想要把信息传回前台,这种方式得不到我们想要的效果了。
常规ajax 调用文件下载也是不可取的,会造成文件乱码。
解决方法:ajax 首先调用一个获取文件路径的方法,根据这个方法的返回值来确定要不要继续下去,如果存在该文件,然后组装form表单,再调用下载的方法即可实现文件下载回显的效果。
$.ajax({
type: "POST",
url: url,
data: params,
success: function(response, status, request) {
var disp = request.getResponseHeader('Content-Disposition');
if (disp && disp.search('attachment') != -1) {
var form = $('<form method="POST" action="' + url + '">');
$.each(params, function(k, v) {
form.append($('<input type="hidden" name="' + k +
'" value="' + v + '">'));
});
$('body').append(form);
form.submit();
}
}
});
以上代码是我在stackoverflow 上面翻到的,按着这个思路,最终实现了我想要的效果,
js:
function download(){
$.ajax({
url:"existFile.do",
method:"post",
data : {
employeeId:$("#download-employeeId").val(),
termId:$("#download-termId").val(),
itemId:$("#download-itemId").val()
},
success: function(response, status, request) {
var employeeId = $("#download-employeeId").val();
var termId = $("#download-termId").val();
var itemId = $("#download-itemId").val();
var disp = request.getResponseHeader('Content-Disposition');
if (response=="fail") {
alert("您未上传工作总结!");
return;
}
var form = $('<form method="POST" action="${baseContextPath }downloadFile.do">');
form.append($('<input type="hidden" name="employeeId" value="'+ employeeId +'">'));
form.append($('<input type="hidden" name="termId" value="'+ termId +'">'));
form.append($('<input type="hidden" name="itemId" value="'+ itemId +'">'));
$('body').append(form);
form.submit();
}
})
}
判断文件是否存在:
/**
* @Describe 判断是否存在工作总结
* @param employeeId 员工id
* @param groupId 小组id
* @return 存在则返回文件路径,不存在返回"fail"
*/
@RequestMapping("existFile")
@ResponseBody
public String test(String employeeId, Integer groupId){
String filePath = null;
GroupVO groupVO = groupService.getDetailGroup(groupId);
String termId = groupVO.getTermId();
String itemId = groupVO.getItemId();
WorkSummaryFile workSummaryFile = workSummaryService.existWorkSummaryFile(employeeId,Integer.parseInt(termId),Integer.parseInt(itemId));
if (null!=workSummaryFile) {
filePath = workSummaryFile.getFilePath();
}else {
return "fail";
}
return filePath;
}
文件下载:
/**
* @Describe 下载个人工作总结
* @param employeeId 员工id
* @param groupId 小组id
* @param response
* @return 下载结果
*/
@RequestMapping("downloadFile")
@ResponseBody
public String downloadSingleFile(@RequestParam("employeeId")String employeeId,
@RequestParam("groupId")String groupId,
HttpServletResponse response){
String filePath = null;
OutputStream outputStream = null;
GroupVO groupVO = groupService.getDetailGroup(Integer.parseInt(groupId));
String termId = groupVO.getTermId();
String itemId = groupVO.getItemId();
WorkSummaryFile workSummaryFile = workSummaryService.existWorkSummaryFile(employeeId,Integer.parseInt(termId),Integer.parseInt(itemId));
if (null!=workSummaryFile) {
filePath = workSummaryFile.getFilePath();
String fileName = workSummaryFile.getOriginalName();
try {
response.addHeader("Content-Disposition","attachment; filename="+ URLEncoder.encode(fileName,"utf-8"));
outputStream = response.getOutputStream();
SClient.downloadFileToStream(filePath, outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}else {
return "fail";
}
return "success";
}