0

我想使用 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);
}

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

我想像这样下载文件: 下载

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

谢谢你

4

1 回答 1

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();
}
于 2013-08-21T11:20:31.553 回答