6 Replies Latest reply on Jun 1, 2010 4:40 PM by joe.knudsen

    File Uploading <s:fileUpload>, data always comes null or 0 length

    ambrish_kumar

      Hi Everyone,


      I am uploading a image file using <s:fileUpload> ,but after submitting the form. I get the data value as Null or of 0 length.


      Here is my file uploading code :




      <!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:a="http://richfaces.org/a4j" 
                      xmlns:c="http://java.sun.com/jstl/core"
                      xmlns:rich="http://richfaces.org/rich"
                      template="layout/template.xhtml">
                        
      <ui:define name="body">
      
      <h:form id="editCatAttribValForm" enctype="multipart/form-data">
                     
           <c:forEach items="#{categoryForProduct}" var="category">
           
           <rich:panel header="#{category.categoryName}">
           
           <h:panelGrid columns="2" border="0">
           
           <c:forEach items="#{attributesMap.get(category.categoryName)}" var="categoryAttrib">
                     
                     <s:decorate template="layout/edit.xhtml">
                      <ui:define name="label">#{categoryAttrib.categoryAttributeName} :</ui:define>
                      
                      <h:inputText size="20"
                            maxlength="60"
                            required="true"
                            rendered="#{categoryAttrib.categoryAttributeType == 'textbox'}"
                            requiredMessage="Value is required"
                            value="#{categoryAttrib.tempAttributeValue}">                
                      </h:inputText>
                                      
                      <h:inputTextarea cols="20" rows="5"
                            required="true"
                            rendered="#{categoryAttrib.categoryAttributeType == 'textarea'}"
                            requiredMessage="Value is required"
                            value="#{categoryAttrib.tempAttributeValue}">                
                      </h:inputTextarea>
                      
                      <s:fileUpload contentType="#{manageProduct.contentType}" 
                          data="#{manageProduct.fileData}" 
                          fileName="#{manageProduct.fileName}" 
                          rendered="#{categoryAttrib.categoryAttributeType == 'imageupload'}"/>
                               
                  </s:decorate>
           
           </c:forEach>
           
           </h:panelGrid>
           
           </rich:panel>
           
           </c:forEach>
      
           <div style="clear:both">
              <span class="required">*Required fields</span>
          </div>
                  
          <rich:spacer height="30"></rich:spacer>  
                  
          <h:commandButton id="updateCatAttrib" 
               value="Update"                          
               action="#{manageProduct.editAttributeValue}" style="cursor:hand;cursor:pointer;">
           </h:commandButton>
                  
          <s:button id="cancelAttrib" 
                value="Cancel"                         
                action="#{manageProduct.cancel}" style="cursor:hand;cursor:pointer;"> 
           </s:button>
           
           
      </h:form>
      
      </ui:define>
      </ui:composition>



      My command buttons are outside the rich panel. Is this an issue because the same file uploading code is working where command buttons are inside the panel?.


      Thanks & Regards


      Ambrish

        • 1. Re: File Uploading <s:fileUpload>, data always comes null or 0 length
          cash1981
          Any reason you are using c:forEach? That might be the reason it is not working. Try replacing with either <a:repeat>, <ui:repeat> or <h:dataTable>, at least on the inner loop.

          I know former jsf versions weren't too crazy about nested tables and forms, but think it is fixed in later versions
          • 2. Re: File Uploading <s:fileUpload>, data always comes null or 0 length
            ambrish_kumar

            Hi Shervin,


            Thanks for the reply.


            I have try both <a:repeat> and <ui:repeat>,but problem is not solved.


            Regards


            Ambrish

            • 3. Re: File Uploading <s:fileUpload>, data always comes null or 0 length
              cash1981

              Please show the manageProduct component java code as well as other relevant code

              • 4. Re: File Uploading <s:fileUpload>, data always comes null or 0 length
                ambrish_kumar

                Here is the code for File uploading:





                @Stateful
                @Scope(ScopeType.CONVERSATION)
                @Name("manageProduct")
                public class ManageProductAction implements IManageProduct {
                
                     //Persistence Context injected from conversation
                     @In
                     EntityManager entityManager;
                     
                     /*
                      * For Image Uploading
                      */
                     private String fileName;
                     private String contentType;     
                     private byte[] fileData;
                     
                     public void persistCategoryLogo(ProductCategoryAttributeValue catAttribValue) {          
                          
                           try {
                                System.out.println("###################### File Data Length-----> "+fileData.length);
                                    
                                if (fileName.equals("") || fileData.length == 0) {
                                    
                                } 
                                else {
                                    Integer extPos;
                                    extPos = fileName.lastIndexOf(".");
                                    String fileType = fileName.substring(extPos);
                                    
                                    if ((fileType.equalsIgnoreCase(".jpg")) || (fileType.equalsIgnoreCase(".png"))
                                              || (fileType.equals(".bmp")) || (fileType.equals(".jpeg"))) {
                                         
                                         StringBuffer dirName = new StringBuffer("");
                                         SeamResourceBundle sRB = new SeamResourceBundle(); 
                                         // getting path from resource bundle
                                         dirName.append(sRB.getString("imageUploadPath"));
                                         dirName.append("categoryLogo");
                                         dirName.append(catAttribValue.getProductMaster().getProductId());
                                         dirName.append("_");
                                         dirName.append(catAttribValue.getCategoryAttributes().getCategoryAttributeId());
                                         dirName.append(fileType);
                                         File saveTo = new File(dirName.toString());
                
                                         // Writing file
                                         FileOutputStream writer = new FileOutputStream(saveTo);
                                         writer.write(fileData);
                                         // new file name
                                         fileName = "/upload_images/"+"categoryLogo" + catAttribValue.getProductMaster().getProductId()+ "_"+ catAttribValue.getCategoryAttributes().getCategoryAttributeId()+ fileType;
                                         // setting companyLogo
                                         catAttribValue.setCategoryAttributeValue(fileName);          
                                    } 
                                    else {
                                         //only jpg,gif,jpeg,png extensions are acceptable
                                         
                                    }
                               }
                          } catch (Exception e) {
                               e.printStackTrace();               
                               
                          }
                     }     
                
                     public void editAttributeValue(){          
                          
                          for(Category category : categoryForProduct){
                               List<CategoryAttributes> catAttributes = attributesMap.get(category.getCategoryName());
                               for(CategoryAttributes catAttrib : catAttributes){
                                    ProductCategoryAttributeValueId catProdAttrValueId = new ProductCategoryAttributeValueId(getInstance().getProductId(),catAttrib.getCategoryAttributeId());
                                    ProductCategoryAttributeValue catProdAttrValue  = entityManager.find(ProductCategoryAttributeValue.class, catProdAttrValueId);
                                    if(catProdAttrValue != null){
                                         if(catAttrib.getCategoryAttributeType().equalsIgnoreCase("imageupload")){
                                              if(fileName != null){
                                                   System.out.println("###################### Before Calling persistCategoryLogo() File Data Length-----> "+fileData.length);//here comes length = 0
                                                   persistCategoryLogo(catProdAttrValue);
                                              }
                                              //catProdAttrValue.setCategoryAttributeValue(persistCategoryLogo(catAttrib));
                                         }else{                         
                                              catProdAttrValue.setCategoryAttributeValue(catAttrib.getTempAttributeValue());
                                         }
                                         getEntityManager().merge(catProdAttrValue);
                                    }else{
                                         ProductCategoryAttributeValue pcav = new ProductCategoryAttributeValue();
                                         pcav.setId(new ProductCategoryAttributeValueId(getInstance().getProductId(),catAttrib.getCategoryAttributeId()));
                                         pcav.setProductMaster(getInstance());
                                         pcav.setCategoryAttributes(catAttrib);
                                         if(catAttrib.getCategoryAttributeType().equalsIgnoreCase("imageupload")){
                                              if(fileName.equals("") || fileName != null){
                                                   System.out.println("###################### File Data Length-----> "+fileData.length);
                                                   persistCategoryLogo(pcav);
                                              }                              
                                              //pcav.setCategoryAttributeValue(persistCategoryLogo(catAttrib));
                                         }else{                         
                                              pcav.setCategoryAttributeValue(catAttrib.getTempAttributeValue());
                                         }
                                         getEntityManager().persist(pcav);
                                    }
                               }
                          }          
                     }
                
                     public String getFileName() {
                          return fileName;
                     }
                
                     public void setFileName(String fileName) {
                          this.fileName = fileName;
                     }
                
                     public String getContentType() {
                          return contentType;
                     }
                
                     public void setContentType(String contentType) {
                          this.contentType = contentType;
                     }
                
                     public byte[] getFileData() {
                          return fileData;
                     }
                
                     public void setFileData(byte[] fileData) {
                          this.fileData = fileData;
                     }
                
                
                     @Remove
                     @Destroy
                     @End
                     public void destroy() {
                          
                     }
                }







                Regards


                Ambrish

                • 5. Re: File Uploading <s:fileUpload>, data always comes null or 0 length

                  did yo manage to solve this problem? I have a similar problem trying to upload pdf files.

                  • 6. Re: File Uploading <s:fileUpload>, data always comes null or 0 length
                    joe.knudsen
                    I forgot the enctype on the h:form and that caused null.

                    <h:form id="fileUploadForm" enctype="multipart/form-data">