Open or save the excel file attachment from the br

2019-08-24 03:00发布

问题:

I am generating the excel file in server side and writing it to the outpuststream with the following response headers.

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment;filename="+fileName);

In my client side : An ajax call to the servlet as shown :

Ext.Ajax.request({
            url: 'GenerateReport',
            method: 'GET',
            params: {
                'start_date': sd.getValue(),
                'end_date':ed.getValue()
            }
 });

Browser shows following Response Headers :

My Issue : The Open and Save dialog prompt is not opening.What may be the problem.Please help me resolve this.Any help is appreciated.Thanks.

回答1:

Try to open new window with appropriate URL instead of AJAX request.



回答2:

Thanks @liya. I have resolved it by using iframe instead of ajax call.Its getting downloaded directly.Here it is :

Ext.core.DomHelper.append(document.body, {
            tag : 'iframe',
            id : 'downloadIframe',
            frameBorder : 0,
            width : 0,
            height : 0,
            css : 'display:none;visibility:hidden;height:0px;',
            src : 'GenerateReport'
   });

But Is their anyway,to open a dialog box before downloading,to prompt for 'OPEN' or 'SAVE' the file instead of saving it directly which I am doing.



回答3:

To be sure the browser would prompt Open or Save, you have to send binary content type. Otherwise the browser tries to open the downloaded file with an associated application. Some browsers (older IE i.e.) might still infer the associated application from the filename extension.

response.setContentType("application/octet-stream");