4 Replies Latest reply on Jun 17, 2010 8:10 AM by vasuki hn

    how to fetch file path while file uploading

    vasuki hn Newbie

      Hi All,


      I am parsing the spreadsheet in my application. I want to get the file path of the spreadsheet which i can send as an parameter to my parsing function.


      I have created a UploadFile.java and uploadfile.xhtml. I will attach my code here.



      package org.domain.user.session;
      
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.AutoCreate;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.log.Log;
      import org.jboss.seam.international.StatusMessages;
      import org.hibernate.validator.Length;
      
      import  turnup.Util;
      
      @Name("uploadFile")
      
      public class UploadFile
      {
          @Logger private Log log;
      
          @In StatusMessages statusMessages;
      
          private String value;
          
      
          
          
      
         public void uploadFile()
          {
              // implement your business logic here
              log.info("uploadFile.uploadFile() action called with: #{uploadFile.value}");
              statusMessages.add("uploadFile #{uploadFile.value}");
              String v2 = value;
              System.out.println(v2);
              Util.processFile(v2);
          }
      
          // add additional action methods
        
          @Length(max = 10000)
          public String getValue()
          {
              return value;
          }
        
          public void setValue(String value)
          {
              this.value = value;
          }
        
      }
      
      
      


      xhtml file



      <!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.org/rich"
          xmlns:a="http://richfaces.org/a4j"
          template="layout/template.xhtml">
      
      <ui:define name="body">
      
          <h:form id="uploadFileForm" enctype="multipart/form-data">
      
              <rich:panel>
                  <f:facet name="header">UploadFile</f:facet>
      
                  <s:decorate id="value" template="layout/edit.xhtml">
                      <ui:define name="label">Browse the file</ui:define>
                      <s:fileUpload id="file" accept="application/*" fileName="#{uploadFile.value}"></s:fileUpload>
                  </s:decorate>
      
                  <div style="clear:both"/>
      
              </rich:panel>
      
              <div class="actionButtons">
                  <h:commandButton id="uploadFile" value="UploadFile"
                          action="#{uploadFile.uploadFile}"/>
              </div>
      
          </h:form>
      
      </ui:define>
      
      </ui:composition>
      



      I dont how to pass the file path which i get when browsing the file to my processfile method.
      I just need only the filepath from front-end to action file(UploadFile.java).
      Please help me in correcting my code.


      Thanks
      Vaasuki.

        • 1. Re: how to fetch file path while file uploading
          Umut Fikret  Gürkavcu Newbie

          Why are you need the filepath ? If you need filedata you can get it from s:fileUpload after page was posted.




            <s:fileUpload data="#{uploadFile.fileData}" accept="application/*" fileName="#{uploadFile.value}" />
               
          
          @Name("uploadFile")
          
          public class UploadFile
          {
          ***
            private byte[] fileData;
            
          ***
           }
          




          May be you can need to add these params inside components.xml for limiting max file size that transfered from the user.




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




               

          • 2. Re: how to fetch file path while file uploading
            vasuki hn Newbie

            Hi Umut,


            Thanks for replying. I need only the file path and the reason is, I need the file path of the browsed file which will be sent as an parameter to the processing method.


            So could you help me in figuring out the solution?


            Thanks
            Vasuki

            • 3. Re: how to fetch file path while file uploading
              Umut Fikret  Gürkavcu Newbie

              As far as i know there is no way to get full path of browsed file for security reasons. In most cases people dont need the path of file and i dont imagine where are you using this file path. Are you using s:fileUpload for only to get path of file ? If this is what you aim s:fileUpload is not capable of getting filepath. Its atrributes are :




                    data — this value binding receives the binary file data. The receiving field should be declared as a byte[] or InputStream (required).
                
                    contentType — this value binding receives the file's content type (optional).
                
                    fileName — this value binding receives the filename (optional).
              
                    accept — a comma-separated list of content types to accept, may not be supported by the browser. E.g. "images/png,images/jpg", "images/*".
              
                    style — The control's style
              
                    styleClass — The control's style class 



              • 4. Re: how to fetch file path while file uploading
                vasuki hn Newbie

                Ok. Then if we use s:fileupload tag inxhtml file for uploading, how do we get the filename at the backend(.java) when we use the filename attribute of the tag?
                Do we need to have a table in our database to store the filename and later fetch using getter/setter methods or do we get the filename directly?


                Could you explain giving an example?


                Thanks
                Vasuki