8 Replies Latest reply on Jun 5, 2009 4:25 PM by Ananthan Rudran

    File Upload

    Ananthan Rudran Newbie

      I am trying to upload a file using s:fileUpload tag. How ever the file conten is always null.


      Here is my web.xml
       


      <filter>
         <filter-name>Seam Multipart Filter</filter-name>
         <filter-class>org.jboss.seam.web.MultipartFilter</filter-class>
          </filter>
         
          <filter-mapping>
             <filter-name>Seam Multipart Filter</filter-name>
             <url-pattern>*.seam</url-pattern>
          </filter-mapping>



      My component.xml


      <component class="org.jboss.seam.web.MultipartFilter">
             <property name="createTempFiles">true</property>
             <property name="maxRequestSize">10000000</property>
             <property name="url-pattern">*.seam</property>
         </component>



      My view,


      ------
      <td colspan="2"><s:fileUpload data="#{sarmgr.fileData.data}"  fileName="#{sarmgr.fileName}" 
      fileSize="#{sarmgr.fileSize}"  accept="" style=" width : 99%;"  /> </td>
      
      ------



      and my Backing EJB is,


      private String fileName;
          private Integer fileSize;
          private byte[] data;
          private String contentType;
      @Begin(join = true, flushMode = FlushModeType.MANUAL)
           public String addTask() {
                if (addedTasks == null) {
                     addedTasks = new ArrayList<OsaTask>(0);
      System.err.println("**************************************:::::::::"+fileData.getData());
       // This always prints null
      


      I am i missing or doing wrongly?


        • 1. Re: File Upload
          Tom Goring Apprentice

          Can you post the whole backing bean...


          maybe in the view you need to reference sarmgr.data rather than sarmgr.fileData.data.


          Also you have your form set to



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


          • 2. Re: File Upload
            Stijn Cremers Novice

            can you post also the whole view.
            You can't do anfile upload with an ajax request.

            • 3. Re: File Upload
              Ananthan Rudran Newbie

              Thanks all for the reply yes i have set the form enctype property still doesnt solve it. any here is my backing bean and view



                <ui:define name="content">
                          <div align = "center">
                            <h:messages styleClass="message"/> 
                         </div>
                         <table style="border : 0px;"> <tr ><td width="100%" colspan="6" ><img src="tablfill.gif"/></td></tr></table>
                  <h:form id="frmsar" enctype="multipart/form-data">
                         <div >
                              <table width="100%" style="border : 0px;">
                             <tr >
                                   <td colspan="8" class="sectionhead">
                                    <h:outputText value="Server Access Request" ></h:outputText>
                                   </td>
                             </tr>
                           </table>
                           <br/>
                           <table width="100%" style="border : 0px;">
                             <tr >
               
                             </tr>
                                 <tr>
                                    <td width="10%"> <h:outputLabel value="SAR Id"  styleClass="label" style=" width : 100%;"/> </td>
                                    <td width="25%"> <h:outputText value="#{sarmgr.osaSarId}" style=" width : 100%;"/></td>
                                    <td width="10%">
                                       <h:outputLabel for="statusMsg" styleClass="label" style=" width : 100%;">Status</h:outputLabel>
                                    </td>
                                    <td width="25%">
                                        <h:outputText id="statusMsg" value="Pending" style=" width : 100%;"></h:outputText>
                                    </td>
                                 </tr>
                                 <tr ><td width="100%" colspan="6" ><img src="rowlfill.gif"/></td></tr>
                                 <tr>
                                 <td width="10%"><h:outputLabel for="listemployee" styleClass="label" value="Requester" style=" width : 80%;"/></td>
                                 <td width="25%"><h:outputText id="listemployee" value="#{currentUser.tblEmployee.name}" style=" width : 80%;"/></td>
                                 <td width="10%"> <h:outputLabel value="Req.For" for="listdeptemployees" styleClass="label" style=" width : 100%;"/> </td>
                                 <td width="25%">
                                          <h:selectOneMenu value="#{employee}" id="listdeptemployees"  style=" width : 100%;"
                                          rendered="#{osamgr.isTsh or osamgr.isHod}" >
                                            <s:convertEntity />
                                           <s:selectItems value="#{deptemployees}" var="emp" label="#{emp.name}" noSelectionLabel="Please Select"/>
                                           </h:selectOneMenu>
                                            <h:outputText value="#{currentUser.tblEmployee.name}"   style=" width : 100%;"
                                          rendered="#{!osamgr.isTsh and !osamgr.isHod}"/>
                                  </td>
                                 </tr>
                                 <tr ><td width="100%" colspan="6" ><img src="rowlfill.gif"/></td></tr>
                                 <tr>
                                    <td width="10%"><h:outputLabel for="txtDateRaised" styleClass="label" style=" width : 100%;">Raised</h:outputLabel> </td>
                                    <td width="15%" >
                                          <h:outputText id="txtDateRaised" value="#{osaSar.dateRaised}"  style=" width : 100%;">  
                                              <f:convertDateTime type="date" pattern="dd/MM/yyyy" timeZone="#{osamgr.timeZone}"/>
                                          </h:outputText> 
                                    </td>
              
                                    <td width="10%"><h:outputLabel for="reqDate" styleClass="label" style=" width : 100%;">Expected</h:outputLabel> </td>
                                    <td width="25%">
                                          <rich:calendar width="60%" id="reqDate" value="#{osaSar.expectedCompletion}" 
                                                 datePattern="dd/MM/yyyy"  direction="bottom-right" />
                                    </td>
              
                                 </tr>
                                <tr>
                                 <td colspan="4">
                                      <table width="100%" style="border : 0px;">
                                        <tr ><td width="100%" colspan="6" ><img src="rowlfill.gif"/></td></tr>
                                   </table> 
                                 </td>
                             </tr>
                                   <tr>
                                     <td colspan="4"  align="center" >
                                       <hr/>
                                     </td>
                                 </tr>
                                 
                                              <tr>
                                 <td colspan="4">
                                      <table width="100%" style="border : 0px;">
                                        <tr ><td width="100%"/></tr>
                                   </table> 
                                 </td>
                             </tr>
                                 
                                <tr>
                                     <td width="10%">
                                       <h:outputLabel for="listAccesType" styleClass="label" style=" width : 100%;">Access</h:outputLabel>
                                    </td>
                                    <td width="25%">
                                        <h:selectOneMenu value="#{accesstype}" id="listAccesType" styleClass="select" style=" width : 85%;">
                                          <s:convertEntity />
                                       <s:selectItems value="#{accesstypes}" var="accesstype" label="#{accesstype.description}" noSelectionLabel="Please Select"/>
                                       </h:selectOneMenu> 
                                    </td>
                               
                                     <td width="10%">
                                       <h:outputLabel for="listtaskaction" styleClass="label" style=" width : 100%;">Action</h:outputLabel>
                                    </td>
                                    <td width="25%">
                                       <h:selectOneMenu value="#{taskaction}" id="listtaskaction" styleClass="select" style=" width : 85%;">
                                         <s:convertEntity />
                                       <s:selectItems value="#{taskactions}" var="taskaction" label="#{taskaction.description}" noSelectionLabel="Please Select"/>
                                       </h:selectOneMenu> 
                                    </td>
                 
                                </tr>
                               <tr ><td width="100%" colspan="6" ><img src="rowlfill.gif"/></td></tr>
                                 <tr>
                                 <td width="10%">
                                     <h:outputLabel for="listtaskreason" styleClass="label" style=" width : 100%;">Reason</h:outputLabel>
                                  </td>
                                   <td width="25%">
                                      <h:selectOneMenu value="#{taskreason}" id="listtaskreason" styleClass="select" style=" width : 85%;">
                                         <s:convertEntity />
                                       <s:selectItems value="#{taskreasons}" var="taskreason" label="#{taskreason.description}" noSelectionLabel="Please Select"/>
                                      </h:selectOneMenu> 
                                   </td>
                                    <td width="10%">
                                     <h:outputLabel for="txtValidity" styleClass="label" style=" width : 100%;">Validity</h:outputLabel>
                                   </td>
                                  <td width="15%">
                                     <h:inputText value="#{osaTask.validity}" id="txtValidity"  maxlength="3"  
                                             style="width:30px" />&#160;<h:outputLabel value="days" /><h:outputLabel style="font-size: 10px;" value=" (Empty or 0 for infinite validity)" />
                                   </td>
                                 </tr>
                                 <tr ><td width="100%" colspan="6" ><img src="rowlfill.gif"/></td></tr>
                                   <tr>
                                     <td width="15%"> <h:outputLabel styleClass="label" style=" width : 100%;">Attachment</h:outputLabel></td>
                                     <td colspan="2"><s:fileUpload  data="#{sarmgr.data}"  fileName="#{sarmgr.fileName}" 
                                            fileSize="#{sarmgr.fileSize}"  accept="" style=" width : 99%;"  /> </td>
                                   </tr>
              


              and the backing EJB is


                @Stateful
              @Name("sarmgr")
              public class SARManager implements ISar {
                private String fileName;
                  private Integer fileSize;
                  private byte[] data;
                  private String contentType;
              @Begin(join = true, flushMode = FlushModeType.MANUAL)
                   public String addTask() {
                        if (addedTasks == null) {
                             addedTasks = new ArrayList<OsaTask>(0);
                        }
                    System.err.println("**************************************:::::::::"+fileData.getData());
              }
                
               }
              


              • 4. Re: File Upload
                Stijn Cremers Novice

                I was looking for the button that submit your form, but i can't find it in your view.
                Make sure that you don't submit with an ajax request.

                • 5. Re: File Upload
                  Daniel Roth Apprentice

                  I assume you have a have something like



                  <h:commandXXXX action="#{sarmgr.addTask}">



                  and


                  getters/setters for data/fileName/fileSize?


                  I get a zero length byte array if I submit a form with no file chosen, so could you try to check that the setData(byte[] data) actually is called (and set to null)?

                  • 6. Re: File Upload
                    Ananthan Rudran Newbie

                    Thanks Roth i will try that way

                    • 7. Re: File Upload
                      Gaurav Chauhan Newbie

                      hey ,
                      did you get this working a i am also facing the same problem

                      • 8. Re: File Upload
                        Ananthan Rudran Newbie

                        Hi,
                        Yes I got it working.Prolem is I was using a4j:commandbutton ( which is Ajax based) When i changed it to h:commandButton it worked. Good luck.