5 Replies Latest reply on Mar 12, 2008 10:55 AM by jaja

    multiple file upload (s:fileUpload)

    jaja

      Hi all,


      I'm trying to upload several files using seam and facelets.
      I've succeeded to upload a single file thanks to this link: http://linuxbeans.blogspot.com/2007/10/image-handling-in-seam-apps-part-ii.html
      however, I failed to do the same with several files.


      here is my code:



      componenets.xml
          <component class="org.jboss.seam.web.MultipartFilter">
              <property name="createTempFiles">true</property>
              <property name="maxRequestSize">1000000</property>
          </component> 
      



      //Action bean
      package com.test.business.action;
      
      import java.util.ArrayList;
      import java.util.List;
      
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Create;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.log.Log;
      
      import com.cat.lc.nfp.business.model.UploadFile;
      
      @Scope(ScopeType.SESSION)
      @Name("testUploadService")
      public class TestUploadAction {
        
        @Logger 
        Log log;
        
        private static final String accept = "text/plain,text/xml,application/xml,application/vnd.ms-excel";
        
        int fileNumber = 5;
        
        private List<UploadFile> files;
        
        public List<UploadFile> getFiles() {
          return files;
        }
      
        public void setFiles(List<UploadFile> files) {
          this.files = files;
        }
      
        public void setFileNumber(int fileNumber) {
          this.fileNumber = fileNumber;
        }
      
        public void upload(){
          display();
        }
        
        @Create
        public void initialize(){    
          files = new ArrayList<UploadFile>(fileNumber);
          for (int i = 0; i < fileNumber; i++) {
            files.add(new UploadFile(i));
          }
        }
        
        private void display(){    
        UploadFile file;
        for (int i = 0; i < files.size(); i++) {
          log.debug("########### processing file #0", i);
          file = files.get(i);
          if(file!=null){
            log.debug("contentType:#0", file.getContentType());
            log.debug("fileName:#0", file.getName());
            log.debug("title:#0", file.getTitle());
            log.debug("size:#0", file.getSize());               
          }
        }
        }
        public int getFileNumber() {
          return fileNumber;
        }
      
        public String getAccept() {
          return accept;
        }
      }
      



      //UploadFileBean_
      import java.io.Serializable;
      
      import org.jboss.seam.annotations.Name;
      
      @Name("uploadFile")
      |public class UploadFile implements Serializable{
           
           private int id;
           private byte[] data;
           private String contentType;    
           private String name;    
           private String title;    
           private int size;
           
           public UploadFile(){
                
           }
      
           public UploadFile(int id){
                this.id=id;
           }
      
           public byte[] getData() {
                return data;
           }
           public void setData(byte[] data) {
                this.data = data;
           }
           public String getContentType() {
                return contentType;
           }
           public void setContentType(String contentType) {
                this.contentType = contentType;
           }
           public String getName() {
                return name;
           }
           public void setName(String name) {
                this.name = name;
           }
           public String getTitle() {
                return title;
           }
           public void setTitle(String title) {
                this.title = title;
           }
           public int getSize() {
                return size;
           }
           public void setSize(int size) {
                this.size = size;
           }
           public int getId() {
                return id;
           }
           public void setId(int id) {
                this.id = id;
           }
      }
      



      //View (.xhtml)
      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
                                   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
        xmlns:s="http://jboss.com/products/seam/taglib"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:rich="http://richfaces.ajax4jsf.org/rich"
        xmlns:a="https://ajax4jsf.dev.java.net/ajax"
        template="../common/layout/template.xhtml">
      
      
        <ui:define name="body">
          <h1><h:outputText
            value="#{messages['data.fileUpload.label.title']}" /></h1>
          <br />
          <br />
      
      
          <h:form enctype="multipart/form-data">
            <a:outputPanel id="table">
              <table border="0">
                <tbody>
      
                  <tr>
                    <th><h:outputText value="Title" /></th>
                    <th><h:outputText value="File" /></th>
                  </tr>
      
                  <a:repeat value="#{testUploadService.files}" var="file">
      
                    <tr>
                      <td>
                          <h:inputText value="#{file.title}" immediate="true" size="50" maxlength="80"/>                    
                      </td>
      
                      <td>
                        <s:fileUpload data="#{file.data}"
                                      accept="#{testUploadService.accept}"
                                      contentType="#{file.contentType}"
                                      fileName="#{file.name}"
                                      fileSize="#{file.size}" size="40"
                                      maxlength="80" />
                      </td>
                    </tr>
                                  
                  </a:repeat>
                    <tr>
                      <td colspan="2" style="text-align: center;">
                          <a:commandButton value="Upload" action="#{testUploadService.upload}"/>                    
                          <a:commandButton value="Cancel" type="reset"/>
                      </td>
                    </tr>
                  
                </tbody>
              </table>
            </a:outputPanel>
            <rich:messages />
            
          </h:form>
        </ui:define>
      </ui:composition>




      The problem is that the files uploaded are usually empty!


      any idea?


      thx in advance.