Jaykumar
Jaykumar

Reputation: 155

Download file using Struts2 with a prompt

I am downloading a file using Struts2. When I click on the button to downlaod the file, the file downloads and opens in Excel by itself rather I want it to download and when I right click on it I should be able to either open or save it. In IE and Firefox I get a window to open or save the file but in Chrome the file file opens in Excel by itself. Is there any way to have it in Chrome just like Firefox and IE

Action

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>

Upvotes: 1

Views: 408

Answers (1)

Andrea Ligios
Andrea Ligios

Reputation: 50281

The prompt asking if opening the file with a desktop application or saving it is given by the contentDisposition HTTP header set to attachment (that you have set). On the contrary, inline would have tried to open the file with a browser plugin, without asking anything.

That said, the reason behind your problem is that you (or someone with access to your computer) have previously instructed Chrome to always open files of this type, and this is why it doesn't ask you anymore about what to do:

enter image description here


The solution is to clear this option from the settings


Customize and Control Google Chrome Settings + Show advanced settings Clear auto-opening settings


enter image description here

Upvotes: 1

Related Questions