File Size

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ken

    File Size

    How can I determine a image file size before uploading it?

    I would like to make sure the size is under a maximum before taking the time
    to upload it.

    If I have to upload the file before determining the size, it could take a
    few minutes (Mbs) on a slow connection before I can determine if the size is
    too large.

    Thanks!

    Ken


  • Ivo

    #2
    Re: File Size

    "Ken" wrote[color=blue]
    > How can I determine a image file size before uploading it?
    >
    > I would like to make sure the size is under a maximum before taking the[/color]
    time[color=blue]
    > to upload it.
    >
    > If I have to upload the file before determining the size, it could take a
    > few minutes (Mbs) on a slow connection before I can determine if the size[/color]
    is[color=blue]
    > too large.[/color]

    Sorry, there is no way you can find out anything about the file to be
    uploaded other than its name, at least with default security settings. Only
    if it 's an image you can try adding it to the current page to read its
    size, width and height.
    --
    Ivo


    Comment

    • Ken

      #3
      Re: File Size

      "Ivo" <no@thank.you > wrote in message
      news:41670c89$0 $46385$cd19a363 @news.wanadoo.n l...[color=blue]
      > "Ken" wrote[color=green]
      > > How can I determine a image file size before uploading it?
      > >
      > > I would like to make sure the size is under a maximum before taking the[/color]
      > time[color=green]
      > > to upload it.
      > >
      > > If I have to upload the file before determining the size, it could take[/color][/color]
      a[color=blue][color=green]
      > > few minutes (Mbs) on a slow connection before I can determine if the[/color][/color]
      size[color=blue]
      > is[color=green]
      > > too large.[/color]
      >
      > Sorry, there is no way you can find out anything about the file to be
      > uploaded other than its name, at least with default security settings.[/color]
      Only[color=blue]
      > if it 's an image you can try adding it to the current page to read its
      > size, width and height.
      > --
      > Ivo[/color]

      This is an image file.
      How do I read the size, width and height of the image using JavaScript when
      it is added to the current page?

      Ken



      Comment

      • Ivo

        #4
        Re: File Size

        "Ken" wrote[color=blue]
        > "Ivo" wrote[color=green]
        > > "Ken" wrote[color=darkred]
        > > > How can I determine a image file size before uploading it?
        > > >[/color][/color]
        > Only[color=green]
        > > if it 's an image you can try adding it to the current page to read its
        > > size, width and height.[/color]
        >
        > This is an image file.
        > How do I read the size, width and height of the image using JavaScript[/color]
        when[color=blue]
        > it is added to the current page?
        >[/color]

        You can add it to the page onchange of the <input type="file"> element,
        using document.create Element('img') or some inerHTML method. Be sure not to
        specify any dimensions of your own or it will be those values that you read
        later on. Then you can access imagereference. height, imagereference. width
        and imagereference. fileSize (in bytes).
        See for a live example (using IE...)
        <url: http://4umi.com/web/javascript/imagewizard.htm >
        --
        Ivo


        Comment

        • Ken

          #5
          Re: File Size

          "Ivo" <no@thank.you > wrote in message
          news:416713ae$0 $62036$ee9da40f @news.wanadoo.n l...[color=blue]
          > "Ken" wrote[color=green]
          > > "Ivo" wrote[color=darkred]
          > > > "Ken" wrote
          > > > > How can I determine a image file size before uploading it?
          > > > >[/color]
          > > Only[color=darkred]
          > > > if it 's an image you can try adding it to the current page to read[/color][/color][/color]
          its[color=blue][color=green][color=darkred]
          > > > size, width and height.[/color]
          > >
          > > This is an image file.
          > > How do I read the size, width and height of the image using JavaScript[/color]
          > when[color=green]
          > > it is added to the current page?
          > >[/color]
          >
          > You can add it to the page onchange of the <input type="file"> element,
          > using document.create Element('img') or some inerHTML method. Be sure not[/color]
          to[color=blue]
          > specify any dimensions of your own or it will be those values that you[/color]
          read[color=blue]
          > later on. Then you can access imagereference. height, imagereference. width
          > and imagereference. fileSize (in bytes).
          > See for a live example (using IE...)
          > <url: http://4umi.com/web/javascript/imagewizard.htm >
          > --
          > Ivo[/color]
          Thanks for the suggestion.

          I am having problems creating the <img The image is not displaying. What
          am I doing wrong?

          Here is the script:
          <input type=file name="picture1" onchange="image (this.value)" >

          <script type="text/javascript">
          function image(field){
          // field=field.rep lace(/(http:\/\/)\1+/g,'$1'); //To learn this, the image
          is on my computer and does not have an http.
          var x=document.crea teElement('img' );
          document.images[1];
          x.src=field;
          }
          </script>
          <script type="text/javascript">doc ument.images[1].click();</script>


          Comment

          • Ivo

            #6
            Re: File Size

            "Ken" wrote[color=blue]
            > "Ivo" wrote[color=green]
            > > "Ken" wrote[color=darkred]
            > > > "Ivo" wrote[/color]
            > > See for a live example (using IE...)
            > > <url: http://4umi.com/web/javascript/imagewizard.htm >[/color]
            >
            > I am having problems creating the <img The image is not displaying. What
            > am I doing wrong?
            >
            > Here is the script:
            > <input type=file name="picture1" onchange="image (this.value)" >
            >
            > <script type="text/javascript">
            > function image(field){
            > // field=field.rep lace(/(http:\/\/)\1+/g,'$1'); //To learn this, the[/color]
            image[color=blue]
            > is on my computer and does not have an http.[/color]

            A single-line comment over two lines... watch out for that! You probably
            want to remove the replace() anyway as, before uploading, all images are
            local.
            [color=blue]
            > var x=document.crea teElement('img' );
            > document.images[1];[/color]

            This is not a complete statement, and you do not need it here.
            [color=blue]
            > x.src=field;[/color]

            OK, if 'field' contains the address of an image, now the image is ready to
            be added to the document, the usual DOM-method for that is appendChild() or
            insertBefore(). Again, see the example page. If that is succesful, the image
            should show at the specified location in the page and you can read its
            properties.
            [color=blue]
            > }
            > </script>
            > <script type="text/javascript">doc ument.images[1].click();</script>[/color]

            --
            Ivo


            Comment

            • Richard Cornford

              #7
              Re: File Size

              Ivo wrote:[color=blue]
              > "Ken" wrote[/color]
              <snip>[color=blue]
              > ... , the image should show at the specified location
              > in the page and you can read its properties.[/color]
              <snip>

              The image, however, originates in a different domain (and has a
              different protocol; file:) to the page in which it may have been
              inserted, So cross-domain/same origin security restrictions are likely
              to apply to the reading of its properties. This, combined with the fact
              that some browsers do not make the information available anyway, makes
              client-side file size determination impossible to achieve cross-browser.

              Richard.


              Comment

              • Ken

                #8
                Re: File Size

                "Richard Cornford" <Richard@litote s.demon.co.uk> wrote in message
                news:ck9b8h$h88 $1$8302bc10@new s.demon.co.uk.. .[color=blue]
                > Ivo wrote:[color=green]
                > > "Ken" wrote[/color]
                > <snip>[color=green]
                > > ... , the image should show at the specified location
                > > in the page and you can read its properties.[/color]
                > <snip>
                >
                > The image, however, originates in a different domain (and has a
                > different protocol; file:) to the page in which it may have been
                > inserted, So cross-domain/same origin security restrictions are likely
                > to apply to the reading of its properties. This, combined with the fact
                > that some browsers do not make the information available anyway, makes
                > client-side file size determination impossible to achieve cross-browser.
                >
                > Richard.[/color]
                I just want to use the file size to make sure it is smaller than a set
                maximum. The image file size will only be use on the viewers browser.

                Is it still considered a different domain? different protocol? if it is used
                on the viewers domain. I do not want to use the size for upload.

                Can you suggest which browsers do make the file size available?

                My problem is:

                If the viewer uploads a file greater the MAX_FILE_Size in PHP, he may have
                to wait for several minutes if the file is size is 3000000. After which,
                PHP set the file size to 0. The viewers then has to start the process over.
                I am looking for a way to make sure the files sizes are under the max file
                size without uploading, i.e. save time and eliminate an annoyance.

                Ken


                Comment

                • Ken

                  #9
                  Re: File Size

                  "Ivo" <no@thank.you > wrote in message
                  news:41681689$0 $30704$18b6e80@ news.wanadoo.nl ...[color=blue]
                  > "Ken" wrote[color=green]
                  > > "Ivo" wrote[color=darkred]
                  > > > "Ken" wrote
                  > > > > "Ivo" wrote
                  > > > See for a live example (using IE...)
                  > > > <url: http://4umi.com/web/javascript/imagewizard.htm >[/color]
                  > >
                  > > I am having problems creating the <img The image is not displaying.[/color][/color]
                  What[color=blue][color=green]
                  > > am I doing wrong?
                  > >
                  > > Here is the script:
                  > > <input type=file name="picture1" onchange="image (this.value)" >
                  > >
                  > > <script type="text/javascript">
                  > > function image(field){
                  > > // field=field.rep lace(/(http:\/\/)\1+/g,'$1'); //To learn this, the[/color]
                  > image[color=green]
                  > > is on my computer and does not have an http.[/color]
                  >
                  > A single-line comment over two lines... watch out for that! You probably
                  > want to remove the replace() anyway as, before uploading, all images are
                  > local.
                  >[color=green]
                  > > var x=document.crea teElement('img' );
                  > > document.images[1];[/color]
                  >
                  > This is not a complete statement, and you do not need it here.
                  >[color=green]
                  > > x.src=field;[/color]
                  >
                  > OK, if 'field' contains the address of an image, now the image is ready to
                  > be added to the document, the usual DOM-method for that is appendChild()[/color]
                  or[color=blue]
                  > insertBefore(). Again, see the example page. If that is succesful, the[/color]
                  image[color=blue]
                  > should show at the specified location in the page and you can read its
                  > properties.
                  >[color=green]
                  > > }
                  > > </script>
                  > > <script type="text/javascript">doc ument.images[1].click();</script>[/color]
                  >
                  > --
                  > Ivo[/color]

                  it is working. I did not have <div id="preview"> over the <img script.

                  Thanks.

                  Ken


                  Comment

                  • Richard Cornford

                    #10
                    Re: File Size

                    Ken wrote:[color=blue]
                    > Richard Cornford wrote:[color=green]
                    >> Ivo wrote:[color=darkred]
                    >> > ... , the image should show at the specified location
                    >> > in the page and you can read its properties.[/color]
                    >> <snip>
                    >>
                    >> The image, however, originates in a different domain (and
                    >> has a different protocol; file:) to the page in which it
                    >> may have been inserted, So cross-domain/same origin security
                    >> restrictions are likely to apply to the reading of its
                    >> properties. This, combined with the fact that some browsers
                    >> do not make the information available anyway, makes
                    >> client-side file size determination impossible to achieve
                    >> cross-browser.[/color][/color]
                    [color=blue]
                    > I just want to use the file size to make sure it is smaller
                    > than a set maximum. The image file size will only be use on
                    > the viewers browser.
                    >
                    > Is it still considered a different domain? different protocol?
                    > if it is used on the viewers domain.[/color]

                    If the script originates on a web server then its protocol will probably
                    be either http: or https:, If you load an image from the user's local
                    hard disk its protocol will be file:. Accessing the properties of an IMG
                    (or Image object) loaded with a different protocol will almost certainly
                    be considered a security violation.
                    [color=blue]
                    > I do not want to use the size for
                    > upload.[/color]

                    That is not exactly what your pervious posts say.
                    [color=blue]
                    > Can you suggest which browsers do make the file size available?[/color]

                    Windows IE browsers (from at least 5.0) certainly provide a fileSize
                    property on IMG elements and Image objects.
                    [color=blue]
                    > My problem is:
                    >
                    > If the viewer uploads a file greater the MAX_FILE_Size in PHP,
                    > he may have to wait for several minutes if the file is size is
                    > 3000000. After which, PHP set the file size to 0. The viewers
                    > then has to start the process over. I am looking for a way to
                    > make sure the files sizes are under the max file size without
                    > uploading, i.e. save time and eliminate an annoyance.[/color]

                    It sounds like telling the user, in as many words, that if they attempt
                    to upload a file bigger than a certain size the server will reject the
                    upload automatically, would solve that problem.

                    Richard.


                    Comment

                    • Ivo

                      #11
                      Re: File Size

                      "Ken" wrote[color=blue]
                      > My problem is:
                      >
                      > If the viewer uploads a file greater the MAX_FILE_Size in PHP, he may have
                      > to wait for several minutes if the file is size is 3000000. After which,
                      > PHP set the file size to 0. The viewers then has to start the process[/color]
                      over.[color=blue]
                      > I am looking for a way to make sure the files sizes are under the max file
                      > size without uploading, i.e. save time and eliminate an annoyance.[/color]

                      It is also the user's problem. If you describe the limitations in the text
                      on the page (which you should anyway), a sensible user will think twice. You
                      cannot "make sure" in a cross-browser and cross-platform manner that
                      javascript is available to begin with.
                      --
                      Ivo


                      Comment

                      • Ken

                        #12
                        Re: File Size

                        "Ken" <kkrolski@wi.rr .com> wrote in message
                        news:sB%9d.6800 1$B51.2178@twis ter.rdc-kc.rr.com...[color=blue]
                        > "Richard Cornford" <Richard@litote s.demon.co.uk> wrote in message
                        > news:ck9b8h$h88 $1$8302bc10@new s.demon.co.uk.. .[color=green]
                        > > Ivo wrote:[color=darkred]
                        > > > "Ken" wrote[/color]
                        > > <snip>[color=darkred]
                        > > > ... , the image should show at the specified location
                        > > > in the page and you can read its properties.[/color]
                        > > <snip>
                        > >
                        > > The image, however, originates in a different domain (and has a
                        > > different protocol; file:) to the page in which it may have been
                        > > inserted, So cross-domain/same origin security restrictions are likely
                        > > to apply to the reading of its properties. This, combined with the fact
                        > > that some browsers do not make the information available anyway, makes
                        > > client-side file size determination impossible to achieve cross-browser.
                        > >
                        > > Richard.[/color]
                        > I just want to use the file size to make sure it is smaller than a set
                        > maximum. The image file size will only be use on the viewers browser.
                        >
                        > Is it still considered a different domain? different protocol? if it is[/color]
                        used[color=blue]
                        > on the viewers domain. I do not want to use the size for upload.
                        >
                        > Can you suggest which browsers do make the file size available?
                        >
                        > My problem is:
                        >
                        > If the viewer uploads a file greater the MAX_FILE_Size in PHP, he may have
                        > to wait for several minutes if the file is size is 3000000. After which,
                        > PHP set the file size to 0. The viewers then has to start the process[/color]
                        over.[color=blue]
                        > I am looking for a way to make sure the files sizes are under the max file
                        > size without uploading, i.e. save time and eliminate an annoyance.
                        >
                        > Ken[/color]
                        Why does filesize = -1 on the first pass and after it runs through a second
                        time, the filesize is correct.

                        What does -1 mean?

                        document.getEle mentById('previ ew').appendChil d(x);
                        wid = document.images[0].width;
                        height = document.images[0].height;
                        status=(documen t.images[0].fileSize)*1;
                        alert(wid + " " + height + " " + wid*height + " " + status); //
                        status = -1
                        wid = document.images[0].width;
                        height = document.images[0].height;
                        status=(documen t.images[0].fileSize)*1;
                        alert(wid + " " + height + " " + wid*height + " " + status); //
                        status = 4.8 mb
                        document.getEle mentById('previ ew').removeChil d(x);
                        Ken



                        Comment

                        • Ken

                          #13
                          Re: File Size


                          "Ken" <kkrolski@wi.rr .com> wrote in message
                          news:lk7ad.1047 06$nA6.61707@tw ister.rdc-kc.rr.com...[color=blue]
                          > "Ken" <kkrolski@wi.rr .com> wrote in message
                          > news:sB%9d.6800 1$B51.2178@twis ter.rdc-kc.rr.com...[color=green]
                          > > "Richard Cornford" <Richard@litote s.demon.co.uk> wrote in message
                          > > news:ck9b8h$h88 $1$8302bc10@new s.demon.co.uk.. .[color=darkred]
                          > > > Ivo wrote:
                          > > > > "Ken" wrote
                          > > > <snip>
                          > > > > ... , the image should show at the specified location
                          > > > > in the page and you can read its properties.
                          > > > <snip>
                          > > >
                          > > > The image, however, originates in a different domain (and has a
                          > > > different protocol; file:) to the page in which it may have been
                          > > > inserted, So cross-domain/same origin security restrictions are likely
                          > > > to apply to the reading of its properties. This, combined with the[/color][/color][/color]
                          fact[color=blue][color=green][color=darkred]
                          > > > that some browsers do not make the information available anyway, makes
                          > > > client-side file size determination impossible to achieve[/color][/color][/color]
                          cross-browser.[color=blue][color=green][color=darkred]
                          > > >
                          > > > Richard.[/color]
                          > > I just want to use the file size to make sure it is smaller than a set
                          > > maximum. The image file size will only be use on the viewers browser.
                          > >
                          > > Is it still considered a different domain? different protocol? if it is[/color]
                          > used[color=green]
                          > > on the viewers domain. I do not want to use the size for upload.
                          > >
                          > > Can you suggest which browsers do make the file size available?
                          > >
                          > > My problem is:
                          > >
                          > > If the viewer uploads a file greater the MAX_FILE_Size in PHP, he may[/color][/color]
                          have[color=blue][color=green]
                          > > to wait for several minutes if the file is size is 3000000. After[/color][/color]
                          which,[color=blue][color=green]
                          > > PHP set the file size to 0. The viewers then has to start the process[/color]
                          > over.[color=green]
                          > > I am looking for a way to make sure the files sizes are under the max[/color][/color]
                          file[color=blue][color=green]
                          > > size without uploading, i.e. save time and eliminate an annoyance.
                          > >
                          > > Ken[/color]
                          > Why does filesize = -1 on the first pass and after it runs through a[/color]
                          second[color=blue]
                          > time, the filesize is correct.
                          >
                          > What does -1 mean?
                          >
                          > document.getEle mentById('previ ew').appendChil d(x);
                          > wid = document.images[0].width;
                          > height = document.images[0].height;
                          > status=(documen t.images[0].fileSize)*1;
                          > alert(wid + " " + height + " " + wid*height + " " + status); //
                          > status = -1
                          > wid = document.images[0].width;
                          > height = document.images[0].height;
                          > status=(documen t.images[0].fileSize)*1;
                          > alert(wid + " " + height + " " + wid*height + " " + status); //
                          > status = 4.8 mb
                          > document.getEle mentById('previ ew').removeChil d(x);
                          > Ken[/color]
                          The answer is the image did not load before the filesize was determined. So
                          I expect the -1 defines null. Interesting though, the width and height were
                          not affected by the delay.

                          The solution is adding a time delay before determining filesize.

                          Ken


                          Comment

                          Working...