如何使用jQuery AJAX和Spring MVC 3从服务器下载文件 [英] How to download file from server using jQuery AJAX and Spring MVC 3
问题描述
我想从服务器实现上载文件的下载(使用AJAX)。在服务器端我写了代码
@RequestMapping(value =/ getInvoice / approvalId / {approvalId},method = RequestMethod.GET)
public
@ResponseBody
byte [] getInvoice(@PathVariable(approvalId)整数approvalId,HttpServletResponse响应)throws IOException {
String fileName = this.approvalService .getFullInvoicePath(approvalId);
文件文件=新建文件(fileName);
response.setContentType(application / octet-stream);
response.setHeader(Content-Disposition,attachment; filename = \+ file.getName()+\);
response.setHeader(Pragma,no-cache);
response.setHeader(Cache-Control,no-cache);
response.setContentLength((int)file.length());
return FileUtils.readFileToByteArray(file);
}
Fiddler2显示回复:
HTTP $ 1.1 $ OK
服务器:Apache-Coyote / 1.1
内容处理:附件; filename =invoice.pdf
Pragma:no-cache
Cache-Control:no-cache
Content-Type:application / octet-stream; charset = UTF-8
内容长度:1028351
日期:2011年7月17日,星期日08:16:41 GMT
%PDF-1.4
%
6 0 obj < / /线性化1 / L 1028351 / O 8 / E 1024254 / N 1 / T 1028185 / H [5056 544]
endobj
xref
6 238
*** FIDDLER:RawDisplay截断为128个字符。右键单击以禁用截断。 ***
如何处理和强制浏览器使用jQuery下载文件?
通常使用两个选项,但不包括Ajax。而且jQuery也不是很好的帮助:
选项1:IFrame
将无声IFrame放入您的页面:
< iframe id =downloadFramestyle =display:none> ;< / iframe中>
当下载应该开始(你没有提到它是如何触发的),使用Javascript可能是jQuery)设置IFrame的URL,这就像你的例子中的 / getInvoice / approvalId / 123
一样:
var iframe = document.getElementById(downloadFrame);
iframe .src =/ getInvoice / approvalId / 123;
设置IFrame网址应触发浏览器显示下载对话框。
选项2:导航到下载URL
第二个选项更简单。只需浏览下载网址即可。一旦浏览器确定它是一个无法显示的MIME类型,它将显示一个下载对话框。
所以当下载被触发时,执行以下Javascript代码: / p>
window.location.href =/ getInvoice / approvalId / 123;
注意:
我不知道所有浏览器是否可靠地呈现带有PDF文件的下载对话框。某些浏览器可能会尝试在浏览器本身中显示它。 内容配置标题是有帮助的,但不能保证。
I want to implement downloading (with AJAX) of uploaded file from server. On the server side I wrote the code
@RequestMapping(value = "/getInvoice/approvalId/{approvalId}", method = RequestMethod.GET)
public
@ResponseBody
byte[] getInvoice(@PathVariable("approvalId") Integer approvalId, HttpServletResponse response) throws IOException {
String fileName = this.approvalService.getFullInvoicePath(approvalId);
File file = new File(fileName);
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setContentLength((int) file.length());
return FileUtils.readFileToByteArray(file);
}
Fiddler2 shows response:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Disposition: attachment; filename="invoice.pdf"
Pragma: no-cache
Cache-Control: no-cache
Content-Type: application/octet-stream;charset=UTF-8
Content-Length: 1028351
Date: Sun, 17 Jul 2011 08:16:41 GMT
%PDF-1.4
%����
6 0 obj <</Linearized 1/L 1028351/O 8/E 1024254/N 1/T 1028185/H [ 5056 544]>>
endobj
xref
6 238
*** FIDDLER: RawDisplay truncated at 128 characters. Right-click to disable truncation. ***
How to handle and force browser to download file using jQuery?
Two options are usually used but neither involves Ajax. And jQuery won't be a great help either:
Option 1: IFrame
Place an invisble IFrame into your page:
<iframe id="downloadFrame" style="display:none"></iframe>
When the download should start (you didn't mention how it is triggered), use Javascript (and possibly jQuery) to set the URL for the IFrame, which is something like /getInvoice/approvalId/123
in your case:
var iframe = document.getElementById("downloadFrame");
iframe .src = "/getInvoice/approvalId/123";
Setting the IFrame URL should trigger the browser to present the download dialog.
Option 2: Navigate to the download URL
The second option is even simpler. Just navigate to the download URL. Once the browser figures out it's a MIME type that cannot be displayed, it will present a download dialog.
So when the download is triggered, execute the following Javascript code:
window.location.href = "/getInvoice/approvalId/123";
Note:
I'm not sure if all browser will reliably present a download dialog with PDF files. Some browsers might try to display it within the browser itself. The content disposition header is helpful but no guarantee.
这篇关于如何使用jQuery AJAX和Spring MVC 3从服务器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!