【问题标题】:Sending file in AJAX response in spring MVC在 Spring MVC 中以 AJAX 响应发送文件
【发布时间】:2015-08-27 03:06:16
【问题描述】:

我正在根据来自客户端的请求在服务器上创建一个文件。现在,我想发送该文件以响应 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.}});

【问题讨论】:

标签: javascript java jquery ajax spring


【解决方案1】:

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

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

但它在 IE 中不起作用。不知道你有什么要求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-11
    • 2012-06-12
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    相关资源
    最近更新 更多