1 Reply Latest reply on Jan 27, 2009 8:43 PM by binnyg

    migrate file upload to <s:fileUpload>

    roxello.roxxello.googlemail.com

      My current file upload functionality is implemented using tomahawk with seam 1.2 on JBoss 4.0.5. I would like to
      replace/update it with the Seam s:fileUpload JSF control,
      Seam 2 and JBoss 4.2


      After a couple of days experimenting I am in serious need
      of some advice on how to proceed. The initial questions I
      can think of are -


      Which JSF implementation will work?
      What jars are required?
      How to configure the web.xml file?


      Any help really appreciated

        • 1. Re: migrate file upload to <s:fileUpload>

          If you want to use s:fileUpload component this is what you should do.


          In components.xml add the following snippet. You can set the max file limit.



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




          In your bean you should define the following properties. You have to strip extra path if client uploads images from IE. IE sends the whole path of the file where as other browsers send the filename.


          @Name("uploadComponent")
          public class CommonUploads {
          
               private byte[] image1;
               private String imageName1;
          
               public byte[] getImage1() {
                    return image1;
               }
          
               public void setImage1(byte[] image1) {
                    this.image1 = image1;
               }
               public String getImageName1() {
                    return imageName1;
               }
                  
                  public void save() {
                     // you have access to the byte array
                  }
          }




          In your xhtml



          <h:form enctype="multipart/form-data">
          <s:fileUpload 
                    id="image1" 
                    data="#{uploadComponent.image1}" 
                    fileName="#{uploadComponent.imageName1}" accept="*.jpg" />
               <h:commandButton value="submit" styleClass="button" action="#{uploadComponent.save}" />
          </h:form>




          You can also add custom error message in your pages.xml


              <exception class="org.jboss.seam.web.FileUploadException">
                  <redirect view-id="#{facesContext.externalContext.requestServletPath}">
                      <message>File upload failed. Max size allowed is 2MB.</message>
                  </redirect>
              </exception>



          Do not forget to rate if this is helpful.