2 Replies Latest reply on Jul 19, 2009 11:41 PM by Shervin Asgari

    s:fileUpload not working

    Atul Mogha Newbie

      Hi,
      I am using s:fileUpload in my program to upload image to the server but the imageData is always null. My web.xml is like this



          <filter>
              <filter-name>Seam Filter</filter-name>
              <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
          </filter>
          <filter-mapping>
              <filter-name>Seam Filter</filter-name>
              <url-pattern>/*</url-pattern>
          </filter-mapping>



      My components.xml is as below:



          <web:multipart-filter max-request-size="5242880" create-temp-files="true" url-pattern="/*"/>
      



      my xhtml is :



                       <h:form id="propertyAddForm" enctype="multipart/form-data">
                           <h:panelGrid columns="2">
                               <s:decorate id="imageField">
                                   <h:outputLabel for="image" styleClass="propDesc">Property Picture:</h:outputLabel>
                                   <s:fileUpload id="image" accept="image/png,image/gif,image/jpeg, image/jpg"
                                                 fileName="#{propertyAction.imageFileName}" fileSize="#{propertyAction.imageFileSize}"
                                                 data="#{propertyAction.imageData}" contentType="#{propertyAction.imageContentType}">
                                       <a4j:support event="onchange" action="#{propertyAction.saveImage}" reRender="imageDisplayField" ajaxSingle="true"/>
                                   </s:fileUpload>
                               </s:decorate>
                               <s:decorate id="imageDisplayField">
                                   <s:graphicImage id="imageDisplay" rendered="#{propertyAction.imageData ne null}" value="#{propertyAction.imageData}"
                                                   fileName="#{propertyAction.imageFileName}" alt="[Property Picture]">
                                   </s:graphicImage>
                               </s:decorate>
                           </h:panelGrid>
                       </h:form>
      


      the backing bean is a statefule bean named propertyAction and below is the code related to fileupload:



          public byte[] getImageData() {
              return imageData;
          }
      
          public void setImageData(byte[] imageData) {
              this.imageData = imageData;
          }
      
          public String getImageContentType() {
              return imageContentType;
          }
      
          public void setImageContentType(String imageContentType) {
              this.imageContentType = imageContentType;
          }
      
          public String getImageFileName() {
              return imageFileName;
          }
      
          public void setImageFileName(String imageFileName) {
              this.imageFileName = imageFileName;
          }
      
          public double getImageFileSize() {
              return imageFileSize;
          }
      
          public void setImageFileSize(double imageFileSize) {
              this.imageFileSize = imageFileSize;
          }
      
           public void saveImage() {
                if (getImageData() != null) {
                     try {
                          Image image = new Image();
                          image.setInput(getImageData());
                          if (image.getBufferedImage() == null) {
                               throw new IOException("The property image data is empty.");
                          }
                          if (!image.getContentType().getMimeType().matches("image/(png|gif|jpeg)")) {
                               facesMessages.addToControl("image",
                                    "Invalid image type: " + image.getContentType());
                          }
                          if (image.getHeight() > 64 || image.getWidth() > 64) {
                               if (image.getHeight() > image.getWidth()) {
                                    image.scaleToHeight(64);
                               } else {
                                    image.scaleToWidth(64);
                               }
                               setImageData(image.getImage());
                          }
                     } catch (IOException e) {
                          log.error("An error occurred reading the profile image", e);
                          facesMessages.addToControl("image", "An error occurred reading the property image.");
                          setImageData(null);
                          setImageContentType(null);
                     }
                }
           }
      





      My work is really stuck because of this. Please help me in this if anyone can.


      Thanks



        • 1. Re: s:fileUpload not working
          Luis Lanzafame Newbie
          You can't use a s:fileUpload with an ajax submit try using a normal submit.
                                       <s:fileUpload id="image" accept="image/png,image/gif,image/jpeg, image/jpg"
                                                     fileName="#{propertyAction.imageFileName}" fileSize="#{propertyAction.imageFileSize}"
                                                     data="#{propertyAction.imageData}" contentType="#{propertyAction.imageContentType}">
                                       </s:fileUpload>
                                       <h:commandButton action="#{propertyAction.saveImage}" />
          
          

          • 2. Re: s:fileUpload not working
            Shervin Asgari Master

            Or if you want to have ajax enabled you should swith to rich:fileUpload or use jQuery to get ajax working.