【问题标题】:Download file using Struts2 with a prompt使用 Struts2 下载文件并提示
【发布时间】:2016-07-28 19:22:38
【问题描述】:

我正在使用Struts2 下载文件。当我单击按钮下载文件时,文件会自行下载并在 Excel 中打开,而不是我希望下载它,当我右键单击它时,我应该能够打开或保存它。在IEFirefox 中,我得到一个打开或保存文件的窗口,但在Chrome 中,文件文件自己在Excel 中打开。有什么办法可以在Chrome 中使用它,就像FirefoxIE 一样

动作

public String viewReport() throws Exception {
    boolean returnReport;
    inputStream = new FileInputStream(DOCUSIGN_REPORT_FILE);

    try {
        returnReport = validateRequest();
        if (returnReport) {

        intgList = this.generateViewIntegrationReportData(getESignUIConfig());
        this.createCSVFile(intgList, DOCUSIGN_REPORT_FILE);

        } else {
            failureResponse(msgs, 400);
            return null;
        }
    } catch (Exception e) {
        e.printStackTrace();
        msgs.add(new Message(ESignatureIntegrationMessageTypeEnum.MESSAGE_TYPE_ERROR, 
                    UiIntegrationKeyConstants.UI_INTEGRATION_ERROR_CODE_500, UiIntegrationKeyConstants.UI_INTEGRATION_ERROR_TEXT_SERVICE_ERROR));
        failureResponse(msgs, 500);
        return null;
    }

    return UiIntegrationKeyConstants.REPORT_REPSONSE;
} 

Struts.xml

<action name="*Integration" method="{1}" class="foo.bar.ESignatureIntegrationAction">
    <result name="success" type="tiles">integrationView</result>  
    <result name="reportResponse" type="stream"> 
        <param name="contentType">application/text/csv</param>
        <param name="inputName">inputStream</param>  
        <param name="contentDisposition">
            attachment;filename="DocuSignEnvelopeReport.csv"
        </param> 
        <param name="bufferSize">4096</param>
    </result>  
</action>

【问题讨论】:

    标签: java google-chrome download struts2 http-headers


    【解决方案1】:

    contentDisposition HTTP 标头设置为attachment(您已设置)给出了询问是否使用桌面应用程序打开文件或保存文件的提示。相反,inline 会尝试使用浏览器插件打开文件,而不询问任何内容。

    也就是说,您的问题背后的原因是您(或有权访问您计算机的人)之前已指示 Chrome 始终打开此类文件,这就是为什么它没有再问你该怎么做:


    解决方案是从设置中清除此选项


    自定义和控制谷歌浏览器 设置 +显示高级设置 清除自动打开设置


    【讨论】:

    • 你太棒了安德里亚。您理解了我的问题,并且没有任何进一步的调查,您就回答了我正在寻找的内容。有人告诉我这是一种浏览器行为,但没有人知道如何修复它。再次感谢您。
    猜你喜欢
    • 2014-05-08
    • 1970-01-01
    • 2012-07-04
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    相关资源
    最近更新 更多