我正在尝试根据此帖子从服务器上使用带有Ext JS 6组件的文件下载文件:通过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():您正在尝试解码无效的JSON字符串:“ first”“ second”“ third”未捕获的错误:您正在尝试解码无效的JSON String:“ first”“ second “ “第三”
似乎有些东西到达了客户端,但它正在尝试将其解析为JSON。尽管如此,如果我将RequestMethod更改为GET并直接在浏览器中键入URL,则会得到该文件,这使我认为标头是正确的。
有人可以帮我解决这个问题,还是您正在使用其他方法/组件从服务器下载文件?
只需使用以下代码即可下载文件:
doExportData: function(config){
window.location.assign(config.url +
(config.params ? '?' + Ext.urlEncode(config.params) : ''));
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句