extjs 4.1使用ajax下载xml文件

时间:2013-08-21 07:06:28

标签: javascript html5 extjs download

我想使用extjs 4.1下载文件。

文件名是“wsnDataModel.xml”。

我尝试过其他帖子中建议的所有内容:

//function invoked clicking a button
DwDataModel : function(th, h, items) {
    //direct method that build in file in the location calculated below with "certurl" (I've verified)
    Utility.GetDataModel(function(e, z, x) {
        if (z.message) {
            //the server method should give an error message
            Ext.create('AM.view.notification.toast', {
                        title : 'Error',
                        html : z.message,
                        isError : true
                    }).show();
        } else {
            // navigate to get data
            var certurl = 'http://' + window.location.host
                    + '/AdminConsole3/' + e;
            Ext.Ajax.request({
                        method : 'GET',
                        url : 'http://' + window.location.host
                                + '/AdminConsole3/' + e,
                        success : function(response, opts) {
//the following navigate and openthe file in the current browser page.
//I don't want to change the current browser page                               
                       //window.location.href = certurl;
//the same behaviour with
                       //document.location = certurl;


                       //and this don't work at all 
                        window.open(certurl,'download');
                        },
                        failure : function(response, opts) {
                            console
                                    .log('server-side failure with status code '
                                            + response.status);
                            console.log('tried to fetch ' + url);
                        }
                    }, this, [certurl]);
        }
    }, th);
}

“导航”重定向应用程序(我不想重定向应用程序),如下所示:redirect

我想下载这个图片的文件: download

我认为这很简单。怎么做?

谢谢

1 个答案:

答案 0 :(得分:2)

很简单:这是成功的功能。

success: function (response, opts) {
    var link = document.createElement("a");
    //this gives the name "wsnDataModel.xml"
    var fileName = certurl.substring(certurl.lastIndexOf('/') + 1);
    link.download = fileName;
    link.href = certurl;
    link.click();
}