我有一個簡單的彈簧控制器安裝程序通過AJAX請求返回文件(尤其是.CSV)。下面是它是運用YUI's骨架,製作標準XHR我的電話:彈簧控制器不返回文件
var downloadFile = Y.all('.downloadLink'); //traverses DOM for all downloadLink <a> tags
downloadFile.on(
'click',
function(e){
var fileName = e.currentTarget.attr('data-id');//retrieves file name
Y.io.request(
'/download/' + fileName,
{
method: 'GET',
on: {
success: function(e) {
alert('success');
},
failure: function(e) {
alert(e.type);
}
}
});
});
這裏是我的控制器:
@RequestMapping(method = RequestMethod.GET, value = "/download/{fileName:.+}")
@ResponseBody
public FileSystemResource download(@PathVariable String fileName) throws IOException {
File file = new File(rootPath + tempDir + '/' + fileName);
try {
if (file.exists()) {
logger.info(fileName + " downloaded");
return new FileSystemResource(file);
} else {
logger.error("Could not download: " + fileName + " does not exist on server");
}
} catch (Exception e) {
logger.error("Error Downloading File");
e.printStackTrace();
}
return null;
}
在本question建議,我試圖通過修改強制下載與
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE
的方法以及包括一個HttpeServletResponse和setti與
response.setContentType("application/force-download");
納克內容類型不設置的MediaType,我的要求是成功的,並且@ResponseBody
我的文件的內容返回給瀏覽器。然而,當我包括我的MediaType收到406:
406 Not Acceptable: The resource identified by this request is only capable
of generating responses with characteristics not acceptable according to the request "accept" headers.
我搜索的文檔,但我還沒有發現其中「接受」,我應該使用參數,使之成爲一個有效的請求(如果單獨將甚至解決這個問題)。任何想法或建議的方法來檢索我的文件?歡呼隊友和復活節快樂
你是如何提出請求?你需要在請求中設置Accept頭,如'Accept:application/octet-stream' – 2015-04-05 20:43:46
好點,我將包括我的AJAX請求 – 2015-04-05 20:45:11