微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 使用Ext JS 6下载文件

我正在尝试使用基于此帖子的Ext JS 6组件从服务器下载文件download a file via Ext js 4

这是组件代码

Ext.define('Aft.view.search.FileDownload', {
extend: 'Ext.Component',
alias: 'widget.acw-fileDownload',
autoEl: {
    tag: 'iframe',
    cls: 'x-hidden',
    src: Ext.SSL_SECURE_URL
},
load: function(config){
    var e = this.getEl();
    e.dom.src = config.url +
        (config.params ? '?' + Ext.urlEncode(config.params) : '');
    e.dom.onload = function() {
        if(e.dom.contentDocument.body.childNodes[0].wholeText == '404') {
            console.
            Ext.create('Acw.view.commons.notifications.Error', {
                html: 'The document you are after can not be found on the server.',
                closeAll: true
            }).show();
        }
    }
}
});

这是获取文件调用

doExportData: function(){
    this.getView().load({
        url: '/aft/faults/download-file'

});

这是Spring rest服务返回一个模拟文件

@RequestMapping(value = "/download-file", method = RequestMethod.POST)
public void downloadFilePost(HttpServletResponse response) {

    String csvFileName = "searchFaults.csv";
    response.setContentType("text/csv");

    // creates mock data
    String headerKey = "Content-disposition";
    String headerValue = String.format("attachment; filename=\"%s\"", csvFileName);
    response.setHeader(headerKey, headerValue);

    try {
        CSVWriter writer = new CSVWriter(response.getWriter(), '\t');
        String[] entries = "first#second#third".split("#");
        writer.writeNext(entries);
        writer.close();
    } catch (IOException e) {
        // Todo Auto-generated catch block
        e.printstacktrace();
    }

}

但是,当我打电话时,我得到这样的东西:

[E] Ext.JSON.decode(): You’re trying to decode an invalid JSON String: “first” “second” “third”
Uncaught Error: You’re trying to decode an invalid JSON String: “first” “second” “third”

似乎某些东西到达客户端,但它试图将其解析为JSON.尽管如此,如果我将RequestMethod更改为GET并直接在浏览器中键入URL,我会得到该文件,这使我认为标题是正确的.

有人可以帮我解决这个问题,或者你是否使用不同的方法/组件从服务器下载文件

解决方法:

只需使用以下代码即可下载该文件

doExportData: function(config){
    window.location.assign(config.url + 
   (config.params ? '?' + Ext.urlEncode(config.params) : ''));
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐