ExtJS的4下载通过Ajax调用的文件 [英] Extjs 4 downloading a file through ajax call

查看:390
本文介绍了ExtJS的4下载通过Ajax调用的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题很简单:我必须下载一个文件,当我提交表单,它可以让我建立与形式,服务器端拍摄数据的文件,然后发送当表单提交一个Ajax调用它为纽带,以警报。事实是,我的老板想要的文件直接而不是通过下载在警报中的链接。所以,我必须确保该文件是可用的服务器端通过龙卷风(网站):

  self.set_header(内容类型,应用程序/八位字节流)
        self.set_header(内容处置,附件;文件名= clients_counter.zip')
        开放(静态/ clients_counter.zip,R)为f:
            而真正的:
                数据= f.read()
                如果没有数据:
                    打破
        self.write(数据)
        self.finish()
 

在服务器端code似乎工作正常,但在客户端(extjs4.1)是一个真正的噩梦。这是我的ajax调用的样子,现在,这是行不通的:

  Ext.Ajax.request({
方法:GET,
网址:'http://whatever.com/count?client='+client+'&start='+start+'&end='+end,
超时:30000,
成功 :
         功能(响应){
    //Ext.Msg.alert(response.responseText);
            desktop.getWindow('算赢)doClose()。
            返回响应;
       } //处理程序,
     失败 :
     功能(响应){
    警报(错误的请求);
    }});
 

解决方案

我有一个类似的问题,试图下载一个Excel文件在Ajax调用我解决了这种方式:

做一个标准的透过,而不是阿贾克斯。

 变种形式= Ext.create('Ext.form.Panel',{//这wolud是你的表单
    standardSubmit:真正的,//这是最重要的组成部分
    网址:../ObtenerArchivoAdjuntoServlet
});

form.submit({
    PARAMS:{
       nombreArchivo:nombreArchivo
    }
});
 

在此,你将能够返回所需的文件。

The problem is very simple: i have to download a file when i submit a form, it's an ajax call when the form is submitted which lets me build a file with the data taken from the form, server side, and then send it as a link to an alert. The fact is that my boss want the file to be downloaded directly and not through a link in an alert. So i had to make sure that the file is available server side through tornado(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()

The server side code seems to work fine, but the client side (extjs4.1) is really a nightmare. This is how my ajax call looks like now, and it doesn't work:

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");
    }});

解决方案

I had a similar problem trying to download an Excel File in an Ajax call I solved it this way:

Make a standard sumbit instead of 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
    }
});

After this you would be able return the desired file.

这篇关于ExtJS的4下载通过Ajax调用的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆