posts - 0, comments - 77, trackbacks - 0, articles - 356
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

ajax struts 动态多文件上传

Posted on 2007-07-30 10:01 semovy 阅读(2661) 评论(5)  编辑  收藏 所属分类: JavaScriptstruts

index.jsp:

<%@ page language="java" pageEncoding="gbk"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html>
<head>
<title>
multiUploadDemo
</title>
</head>
<script language="javascript">
   var num = 0;
 
 function upload(){
  document.getElementById("status").innerHTML = "文件上传中...";
     multiUploadForm.submit();
   }

function additem(id)
{
 var row,cell,str;
 row = eval("document.all["+'"'+id+'"'+"]").insertRow();
 if(row != null )
    {
       cell = row.insertCell();
       str="<input type="+'"'+"file"+'"'+" name=uploadFile["+ num +"].file><input type="+'"'+"button"+'"'+" value="+'"'+"删除"+'"'+" onclick='deleteitem(this,"+'"'+"tb"+'"'+");'>"
      cell.innerHTML=str;
    }
 num++;
}
function deleteitem(obj,id)
{
 var rowNum,curRow;
 curRow = obj.parentNode.parentNode;
 rowNum = eval("document.all."+id).rows.length - 1;
 eval("document.all["+'"'+id+'"'+"]").deleteRow(curRow.rowIndex);
}
function callback(msg)
{
 document.getElementById("status").innerHTML = "文件上传完成...<br>" + msg;
}
</script>
<body bgcolor="#ffffff">
<div id="status"></div>
<html:form method="post" action="/multiUpload.do" enctype="multipart/form-data" target="hidden_frame">

<table id="tb">
</table>

</html:form>
<iframe name='hidden_frame' id="hidden_frame" style="display:none"></iframe>
<input type="button" name="btnAddFile" value="Add File" onclick="additem('tb')"/>
<input type="button" name="btnUpload" value="upload" onclick="upload()"/>
</body>
</html:html>

Struts config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="multiUploadForm" type="com.yourcompany.struts.MultiUploadForm" />

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="multiUploadForm"
      name="multiUploadForm"
      path="/multiUpload"
      scope="request"
      type="com.yourcompany.struts.MultiUploadAction"
      validate="false" />

  </action-mappings>

  <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>

Form bean:  MultiUploadForm.java


package com.yourcompany.struts;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts.action.ActionForm;

public class MultiUploadForm extends ActionForm {
 private  List  myFiles; 
    public  MultiUploadForm(){ 
            myFiles  =  new  ArrayList(); 
            //为了能够在页面初始显示一个file 
            myFiles.add(new  UploadFile()); 
    } 
    public  List  getMyFiles()  { 
            return  myFiles; 
    }         

//注意这个方法的定义 

  public  UploadFile  getUploadFile(int  index){ 
            int  size  =  myFiles.size(); 
            if(index>size-1){ 
                    myFiles.add(new  UploadFile()); 
            } 
            return  (UploadFile)myFiles.get(index); 
    } 
    public  void  setMyFiles(List  myFiles)  { 
            this.myFiles  =  myFiles; 
    } 

}


ActionBean :MultiUploadAction.java

package com.yourcompany.struts;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

public class MultiUploadAction extends Action {
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  MultiUploadForm multiUploadForm = (MultiUploadForm) form;
  List myFiles = multiUploadForm.getMyFiles();
  String fileStr = "";
  for (int i = 0; i < myFiles.size(); i++) {
   UploadFile uploadFile = (UploadFile) myFiles.get(i);
   FormFile file = uploadFile.getFile();

   if (file == null) {
    System.out.println("file  is  null");
   } else {

    // 能运行到这里,就可以使用单个文件上传的方法进行上传了。循环而已
    System.out.println("filename:::" + file.getFileName());
    System.out.println("file  size:::" + file.getFileSize());
    fileStr += "filename:::" + file.getFileName() + "file  size:::" + file.getFileSize() ;
     
   }
  }
  //返回文本
  try
  {
   response.setHeader("ContentType", "text/html;charset=gbk");
   PrintWriter out = response.getWriter();
   out.write("<script>parent.callback('upload file success" + fileStr + "')</script>");
  }catch(IOException e)
  {
   e.printStackTrace();
  }

  return null;
 }

}


Bean: UploadFile.java

package com.yourcompany.struts;

import java.io.Serializable;

import org.apache.struts.upload.FormFile;

public class UploadFile implements Serializable {
  private  FormFile  file; 
     public  FormFile  getFile()  { 
             System.out.println("run  in  uploadFile.getFile()"); 
             return  file; 
     } 
     public  void  setFile(FormFile  file)  { 
             this.file  =  file; 
     } 
}


评论

# re: ajax struts 动态多文件上传[未登录]  回复  更多评论   

2007-09-28 04:57 by aa
为什么没有初始显示一个file

# re: ajax struts 动态多文件上传  回复  更多评论   

2008-06-19 17:05 by dddd
没看出来哪里有ajax的影子

# re: ajax struts 动态多文件上传  回复  更多评论   

2008-07-30 13:12 by sdf
sssssss

# re: ajax struts 动态多文件上传  回复  更多评论   

2008-08-27 10:15 by 防盗防
没看出来哪里有ajax的影

# re: ajax struts 动态多文件上传  回复  更多评论   

2008-10-03 14:16 by error
copy&paste

只有注册用户登录后才能发表评论。


网站导航: