一个Form一次操作执行多次提交

7 篇文章 0 订阅
3 篇文章 0 订阅

一个Form一次操作执行多次提交

2009-04-30

 

       20号开始加入了流程开发小组,一开始是先配那个WPS的流程处理环境,结果 没有配置成功,但他们又赶紧项目的工期,一开始也没有足够的时间再支持我去配这个流程的环境了,只有等到五一以后了,所以就让我帮他们开发其它的相关功能。

       第一个功能就是就一在个Form表单中,选中多个复选框(CheckBox),每一个checkBox代表一个要工单的附件信息,现在需要根据选择的附件进行同时多文件下载。这样的话呢,用传的下载方式 就很难以实现了,我开始在是在Action中做了多文件读取,循环向客户端传送数据,但很遗憾失败了,普通的Form表单提交,只能相当于一个请求,所以提交一次只能下载一个文件。

      

       后来跟同事进行讨论,参考了以前做过的项目,有过类似的功能实现。所以就尝试了起来,结果成功了!^_^

      

       具体实现文件如下:

1.       添加JS 来响应Form表单的 Submit

/**

        * 下载附件

       **/

       function doDownload(){

           var checkboxes = document.all.shetdFiles

           var nLen = checkboxes.length;

            

              for(var i=0;i<nLen;i++){

              var checkbox = checkboxes[i];

              if(checkbox.checked){

               var url = "<%=rootPath%>/sheet/NrmbsDownload.action?shetdFiles="+checkbox.value;

                  window.frames["iuframe"+checkbox.value].location.href=url;

              }

               

               }

           }

   

 

2.       Form表单中添加 Iframe ,相当于每一个Checkbox 对应着一个 Iframe , IFramename属性进行动态生成,这样就可以跟Checkbox动态的关联起来了。

如:(注意iframe的属性设置)

<td><input type="checkbox" name="shetdFiles" value="${id}"  /></td>

                                       <iframe id="frm${id}" name="iuframe${id}"  width="0" height="0" scrolling="no" frameborder="0"></iframe>

 

3.       接着是Action 的响应了,代码如下:

 

 

public String  download(){

      

 

       this.getResponse().reset();

       this.getResponse().setCharacterEncoding("UTF-8");

      

       if(shetdFiles != null){

           for(String shtfileId : shetdFiles){

              SheetFile shtFile = sheetFileService.getSheetFile(shtfileId);

              if(shtFile != null){

                  String fileName;

                  try

                  {

                     fileName = URLEncoder.encode(shtFile.getRealName(), "UTF-8");

                     this.getResponse().setHeader("Content-Disposition",

                            "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "GBK"));

                     this.getResponse().setContentType("application/msexcel");

                     String filePath = NrmbsConsts.UPLOAD_PATH;

                     File file = new File(filePath + File.separator + shtFile.getFileName());

                     InputStream is = new FileInputStream(file);

                     OutputStream os = this.getResponse().getOutputStream();

                     int bytesRead = 0;

                     byte[] buffer = new byte[8192];

                     while ((bytesRead = is.read(buffer, 0, 8192)) != -1)

                     {

                         os.write(buffer, 0, bytesRead);

                     }

                     os.flush();

                     is.close();

                     os.close();

                    

                  }

                  catch (Exception e)

                  {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

                  }

 

                 

              }

           }

       }

      

       return null;

    }

 

 

 

完。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值