我正在尝试开发一种功能,以便在春季通过ajax调用下载文件。我知道已经有很多问题了,我已经详细研究了这些问题,并提出了以下建议,但是文件下载仍无法进行。
我在Ext.js中的ajax调用:
downloadFile:function(){
var me = this,
constant = Kynox.constant;
console.log("hello");
Ext.Ajax.request({
url: "/protected/impoccurences/downloadSelectedOccurence.do",
method: 'GET',
params: {
occurenceId: me.getSelectedRecord().getId()
},
success: function(response){
console.log("success: " + response.responseText);
console.log(new Blob([response]));
},
failure: function(response, opts){
console.log("server-side failure with status code: " + response.status);
}
});
}
和控制器:
@RequestMapping(value="/protected/impoccurences/downloadSelectedOccurence", method=RequestMethod.GET)
public ModelAndView downloadSelectedFile(HttpServletRequest request, HttpServletResponse response) {
ServletOutputStream outputStream = null;
try {
int occurenceId = ServletRequestUtils.getRequiredIntParameter(request, "occurenceId");
ImpOccurence impOccurence = (ImpOccurence) service.selectSource(Integer.toString(occurenceId));
byte[] fileContent = impOccurence.getSourceFileOther();//this retrieves the data
String fileName = impOccurence.getFileName();
String fileType = "." + FilenameUtils.getExtension(fileName.toLowerCase());
DocContentType docContentType = (DocContentType) docContentTypeDao.findBy("Extension", fileType);
outputStream = response.getOutputStream();
String contentType = docContentType.getContentType();
response.setContentType(docContentType==null?null:contentType);
response.setHeader("Content-disposition","attachment; filename=\""+fileName+"\"");
outputStream.write(fileContent);
outputStream.flush();
outputStream.close();
return null;
}catch(Exception ex){
daoLogger.error(ex.getMessage(), ex);
throw new RuntimeException(ex);
}finally {
try {
if(outputStream != null) {
outputStream.close();
}
}catch(Exception ex) {
daoLogger.error("Download error during closing resources: " + ex);
}
}
}
这就是我在控制台中看到的内容:
hello
success: stuffer n�2
Blob(15) {size: 15, type: ""}
我该如何进行这项工作?预先感谢。