问题很简单:当我提交表单时我必须下载一个文件,这是一个ajax调用,当提交表单时,我可以使用从表单,服务器端获取的数据构建一个文件,然后将其作为一个链接到提醒.事实是我的老板希望直接下载文件,而不是通过警报中的链接.所以我必须通过龙卷风(web)确保文件在服务器端可用:
self.set_header('Content-Type', 'application/octet-stream') self.set_header('Content-Disposition', 'attachment; filename=clients_counter.zip') with open("static/clients_counter.zip", 'r') as f: while True: data = f.read() if not data: break self.write(data) self.finish()
服务器端代码似乎工作正常,但客户端(extjs4.1)真的是一场噩梦.这就是我的ajax调用现在的样子,它不起作用:
Ext.Ajax.request({ method : "GET", url : 'http://whatever.com/count?client='+client+'&start='+start+'&end='+end, timeout : 30000, success : function (response) { //Ext.Msg.alert(response.responseText); desktop.getWindow('count-win').doClose(); return response; }//handler, failure : function(response) { alert("Wrong request"); }});
sakadas.. 9
在阅读了Ext JS论坛的各种资源以及stackoverflow中的各种资源后,下面是我选择的方法(使用Ext JS 4.2.1版):
downloadFile: function(config){ config = config || {}; var url = config.url, method = config.method || 'POST',// Either GET or POST. Default is POST. params = config.params || {}; // Create form panel. It contains a basic form that we need for the file download. var form = Ext.create('Ext.form.Panel', { standardSubmit: true, url: url, method: method }); // Call the submit to begin the file download. form.submit({ target: '_blank', // Avoids leaving the page. params: params }); // Clean-up the form after 100 milliseconds. // Once the submit is called, the browser does not care anymore with the form object. Ext.defer(function(){ form.close(); }, 100); }
Ryuk.. 5
我在尝试在Ajax调用中下载Excel文件时遇到了类似的问题,我通过这种方式解决了这个问题:
制作标准sumbit而不是Ajax.
var form = Ext.create('Ext.form.Panel', { // this wolud be your form standardSubmit: true, // this is the important part url: '../ObtenerArchivoAdjuntoServlet' }); form.submit({ params: { nombreArchivo: nombreArchivo } });
在此之后,您将能够返回所需的文件.