7 Replies Latest reply on Apr 26, 2009 11:19 AM by -==cfSearching==-

    Checking File Size with Java

      I am trying to upload a zip file using cfzip in an application I am working on. I am using Java to get the files size before the file is uploaded however the results of the "scan" and the size of the fiel are completely different and I was hoping someone could point me to what is wrong.

       

      Here is the java code

       

          <!--- check the size of the zip file --->
          <cfif len(form.zipGallery)>
              <cfset f = createObject("java","java.io.File").init(form.ZipGallery)>
              <cfif f.length() gt (25 * 2048)>
                  <cfset valError = listAppend(valError,"That file is too large. Please make you zip file less than 25MB" & "-" & #f.length()#)>
              </cfif>
          </cfif>

       

      The file is  23.6 MB and it returns a value of 24804509.

       

      What am I missing?

       

      Thanks in advance.

        • 1. Re: Checking File Size with Java
          tooMuchTrouble Level 3

          the actual file size is 23.65542316436767578125 mb or something like that.

           

          the length() method returns bytes. divide by (1024 * 1024) to get mb.

          • 2. Re: Checking File Size with Java
            -==cfSearching==- Level 4

            RandomActsDesign wrote: ...

             

            I am using Java to get the files size before the file is uploaded however the results of the "scan" and the size of the fiel are completely

             

            Well .. technically the file was already uploaded.  It is sitting in a temp directory on the server.  cffile action=upload just moves the temp file to its final destiation and restores the file extension.

            • 3. Re: Checking File Size with Java
              Level 1

              Now I'm just a tad confused. That little bit of code is on the validation page. The upload doesn't even happen until it reaches the processing page so how could it already be on the server.

               

              Here is how I make my forms. I find it easier to keep each portion seperate for future changes. The form validation page is where the file check is. If it finds any errors it brings the input page back up. If not the processing page is run.

               

              <cfswitch expression="step">

                  <cfcase value="process">

                        <cfinclude template="formValidation.cfm">

                  </cfcase>

                   <cfdefaultcase>

                        <cfinclude template="formInput.cfm">

                   <cfdefaultcase>

              </cfswitch>

              • 4. Re: Checking File Size with Java
                -==cfSearching==- Level 4

                RandomActsDesign wrote:

                 

                Now I'm just a tad confused. That little bit of code is on the validation page. The upload doesn't even happen until it reaches the processing page so how could it already be on the server.

                 

                When your typical form is submitted to the server, everything, including files, are uploaded with it. Any files submitted are placed in a temp directory. (You can see the path by cfdump'ing the FORM scope). When you run cffile action=upload, the file(s) are simply moved from the temp directory to another location and renamed.

                 

                If you think about it, CF runs on your server. It has no way to access files on a user's machine.  Can you imagine if it did? That would mean any web site could access files on your computer at will, which would be a big security hole.  So there is no way CF (or your server side java)  could know the file size unless that file is already uploaded to your server.

                • 5. Re: Checking File Size with Java
                  BKBK Adobe Community Professional & MVP

                  RandomActsDesign wrote:

                  Checking File Size with Java

                   

                  Why the extra expense of creating a Java object when you can just obtain the file size from cffile.filesize?

                  • 6. Re: Checking File Size with Java
                    Dan Bracuk Level 5

                    I've not tried it, but this looks like it might work.  http://www.webdeveloper.com/forum/archive/index.php/t-53380.html

                     

                    vbscript does run on the client I presume.

                     

                    Note the caveat about IE.  If this is a public site, you'll have to test it on firefox as well, plus on a mac.

                    • 7. Re: Checking File Size with Java
                      -==cfSearching==- Level 4

                      BKBK wrote:

                       

                      RandomActsDesign wrote:

                      Checking File Size with Java

                       

                      Why the extra expense of creating a Java object when you can just obtain the file size from cffile.filesize?

                       

                      Yes, I see I left that out of the explanation.  I suspect they were using a java object because they did not realize the file was already on the server, so you might as well just do a cffile upload.