如何使用 jQuery AJAX 和 Spring MVC 3 从服务器下载文件 [英] How to download file from server using jQuery AJAX and Spring MVC 3

查看:40
本文介绍了如何使用 jQuery AJAX 和 Spring MVC 3 从服务器下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现从服务器下载(使用 AJAX)上传的文件.在服务器端我写了代码

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 显示响应:

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. ***

如何处理和强制浏览器使用 jQuery 下载文件?

How to handle and force browser to download file using jQuery?

推荐答案

通常使用两个选项,但都不涉及 AJAX.jQuery 也不会帮上大忙.

Two options are usually used but neither involves AJAX. And jQuery won't be a great help either.

在您的页面中放置一个不可见 IFrame:

Place an invisible IFrame into your page:

<iframe id="downloadFrame" style="display:none"></iframe>

当下载应该开始时(你没有提到它是如何触发的),使用 Javascript(可能还有 jQuery)来设置 IFrame 的 URL,类似于 /getInvoice/approvalId/123在你的情况下:

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";

设置 IFrame URL 应该会触发浏览器显示下载对话框.

Setting the IFrame URL should trigger the browser to present the download dialog.

第二个选项更简单.只需导航到下载 URL.一旦浏览器发现它是一个无法显示的 MIME 类型,它就会显示一个下载对话框.

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.

所以当下载被触发时,执行以下 JavaScript 代码:

So when the download is triggered, execute the following JavaScript code:

window.location.href = "/getInvoice/approvalId/123";

<小时>

注意

我不确定是否所有浏览器都能可靠地显示包含 PDF 文件的下载对话框.某些浏览器可能尝试在浏览器本身中显示它.Content-Disposition HTTP 标头很有帮助,但不能保证.


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 HTTP header is helpful but no guarantee.

这篇关于如何使用 jQuery AJAX 和 Spring MVC 3 从服务器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆