在Spring MVC中发送文件的AJAX响应

时间:2015-08-27 03:06:17

标签: javascript java jquery ajax spring

我正在客户端的请求中在服务器上创建一个文件。现在,我想发送该文件以响应AJAX响应。以下是JAVA代码。

response.reset();
            response.setContentType("application/pdf"); 
            response.setHeader("Content-disposition", "attachment; filename=\"Portfolio.pdf\""); 
            OutputStream output;
            try {
                output = response.getOutputStream();
                output.write(Util.readFileInBytes("/Portfolio.pdf"));
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

现在,如何向用户显示“另存为”对话框以保存文件。在此先感谢您的帮助。
javascript代码如下:

$.ajax({
            url : "export",
            dataType : 'text',
            contentType : 'application/pdf',
            success: function() { //code to display "Save As" dialog box to user.}});

1 个答案:

答案 0 :(得分:0)

另请参阅此讨论stackoverflow.com/questions/833068/how-to-force-save-as-dialog-box-in-firefox-besides-changing-headers

您可以在 base64 字符串中对文件进行编码,将其发送到客户端,然后将值分配给 location.href ,并向用户显示保存对话框。

但它不适用于 IE 。不知道你的要求是什么。