我想用servlet 和ext实现文件下载,
js代码为         items: ['->',{
           text: '导出',
           handler: function(){
         Ext.MessageBox.confirm('提示','确定要导出网点吗?',function(btn){
         var requestObj = gridFormPanel.form.getValues();
         if (btn == "yes") {
         var form = gridFormPanel.form;
         var requestObj = gridFormPanel.form.getValues();
        
                        form.submit({
                         waitMsg:"数据处理中,请等待...",
//                          params:Ext.JSON.encode(requestObj),
                 url: THREETI.EXPORT_FILE_URL + "sName=/exportSuperItemExcel&&shopCategory=0",
                 method: 'POST'
         }
         })
              }
         }]后台servlet代码是
 String fileName = result.getString("path");
 response.reset();  
 response.setCharacterEncoding("UTF-8");
 response.setContentType("application/vnd.ms-excel");  
 response.setHeader("Content-Disposition", "attachment; filename=\""+ fileName + "\"");
 
 ServletOutputStream sos = response.getOutputStream();  
//  PrintWriter writer = response.getWriter();
 BufferedInputStream fin = new BufferedInputStream(new FileInputStream(fileName));  
 byte[] content = new byte[1024];  
 int length;  
 while ((length = fin.read(content, 0, content.length)) != -1) {  
     sos.write(content, 0, length);  
 }  
 fin.close();  
 sos.flush();  
 sos.close();  
 
//  writeResponse(response, result.toString());我预想是表单提交后,生成文件,传到前台,前台自动会弹出下载框,但是现在根本没反应,求各位大神帮帮忙,在线等啊java ext

解决方案 »

  1.   

    location.href = 你的URL  就行
      

  2.   

    这个location.href 在js具体怎么写?
      

  3.   

    进去了
    response里也有值
                            form.submit({
                             waitMsg:"数据处理中,请等待...",
    //                          params:Ext.JSON.encode(requestObj),
                     url: THREETI.EXPORT_FILE_URL + "sName=/exportSuperItemExcel&&shopCategory=0",
                     method: 'POST',
                         success : function(form, action) {
                             alert(action.response.responseText);
    //                          var obj = Ext.JSON.decode(action.response.responseText);
    //                          alert(obj.path);
    //                          window.open(obj.path, '_blank', 
    //                                 'width=1,height=1,toolbar=no,menubar=no,location=no');//报错,提示“拒绝访问”
                 },
                 failure : function(form, action) {
                 //window.open('test.html','newwindow','height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no')
                 alert(action.response.responseText);
                 alert("failure");
                 }
                            });
    这里直接进入了failure,responseText里有很长一串乱码的东西
      

  4.   

    //你excel 名字先用个英文的试试看。
            resp.setHeader("Cache-Control", "no-cache");  
            resp.setContentType("application/vnd.ms-excel");  
            resp.setHeader("Content-Disposition", "attachment; filename=file.xls");  
            OutputStream os = resp.getOutputStream();  
            FileInputStream in = new FileInputStream(new File("xxxxx"));  
            int n = 0;// 每次读取的字节长度  
            byte[] bb = new byte[1024];// 存储每次读取的内容  
            while ((n = in.read(bb)) != -1) {  
                os.write(bb, 0, n);// 将读取的内容,写入到输出流当中  
            }  
            os.close();// 关闭输入输出流  
            in.close();  
      

  5.   

    我现在改成aaa.xls,前台还是不会弹出下载框
    感觉文件内容已经放到response里了,但是前台有问题
      

  6.   

    刚才我查了一下,ext的form.submit是以Ajax方式提交的,是不是这种方式不会弹出下载框?
      

  7.   

    你试试直接链接url下载看看可以不?
      

  8.   

    ajax 是可以的。success 后。重新打开的方式实现
      

  9.   

    搞定了 ,是因为ext表单提交默认为ajax提交,所以不会弹出下载框
    在Ext 4.2里,我设置一下form表单属性var gridFormPanel = Ext.create('Ext.FormPanel', { standardSubmit:true}),这样表单提交会以正常方式提交,就会弹出下载框了,搞了我2天了,心都肿了,现在贴出我的代码
    String fileName = result.getString("path");
     File file = new File("E://"+ fileName);
     
     response.reset();  
     response.setCharacterEncoding("UTF-8");
     response.setContentType("application/x-msdownload");  
     response.setContentLength((int)file.length());
     response.setHeader("Content-Disposition", "attachment; filename="+ fileName);
     
     OutputStream out = response.getOutputStream();
     BufferedInputStream buff = new BufferedInputStream(new FileInputStream(file)); 
     byte[] content = new byte[1024]; 
     long k = 0;
     while(k<file.length()){
                int j=buff.read(content,0,1024);
                k+=j;
                out.write(content,0,j);      }
     out.flush();  
     out.close();
     buff.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

    js代码
    var gridFormPanel = Ext.create('Ext.FormPanel', { standardSubmit:true}
               handler: function(){
             Ext.MessageBox.confirm('提示','确定要导出网点吗?',function(btn){
             var requestObj = gridFormPanel.form.getValues();
             if (btn == "yes") {
             var form = gridFormPanel.form;
             var requestObj = gridFormPanel.form.getValues();
            
                            form.submit({
    //                          waitMsg:"数据处理中,请等待...",
    //                          params:Ext.JSON.encode(requestObj),
                     url: THREETI.EXPORT_FILE_URL + "sName=/exportSuperItemExcel&&shopCategory=0",
                     method: 'POST'
                            });
             }
             })
                  }
    贴出来后希望对别人有帮助,我真是被搞死了
      

  10.   

    重新打开方式的话,是用window.open方式么?这种方式的话新 弹出一个网页,这样应该怎么做呢?
    可以贴出来么?
    主要我是用servlet来做的,response写完文件流后,无法再返回json格式的数据
                            form.submit({
    //                          waitMsg:"数据处理中,请等待...",
    //                          params:Ext.JSON.encode(requestObj),
                     url: THREETI.EXPORT_FILE_URL + "sName=/exportSuperItemExcel&&shopCategory=0",
                     method: 'POST',
                         success : function(form, action) {
                             alert(action.result.msg);
                             alert(action.response.responseText);
    //                          var obj = Ext.JSON.decode(action.response.responseText);
    //                          alert(obj.path);
    //                          window.open(obj.path, '_blank', 
    //                                 'width=1,height=1,toolbar=no,menubar=no,location=no');//报错,提示“拒绝访问”
                 },
                 failure : function(form, action) {
                 alert(action.failureType);
                 alert(action.result.msg);
                 alert(action.response.responseText);
                 alert("failure");
                 }
                            });
    所以不能设置success的值,直接返回到failure里了,然后在failure里应该怎么写?
      

  11.   

    不知道楼主解决没有,我的是因为我生成excel时,没有记录时生成文件有问题导致的,楼主加油咯