Open or save the excel file attachment from the br

2019-08-24 02:51发布

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 : enter image description here

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.

3条回答
看我几分像从前
2楼-- · 2019-08-24 03:13

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

查看更多
叛逆
3楼-- · 2019-08-24 03:17

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");

查看更多
贼婆χ
4楼-- · 2019-08-24 03:37

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.

查看更多
登录 后发表回答