Open or save the excel file attachment from the browser using ajax request in Ext.js

六眼飞鱼酱① 提交于 2019-12-12 02:48:59

问题


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



来源:https://stackoverflow.com/questions/18609915/open-or-save-the-excel-file-attachment-from-the-browser-using-ajax-request-in-ex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!