FileSize - image

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

    FileSize - image

    I would like to measure the fileSize of the image (without uploading it -
    php).

    I use:
    var size_pic = document.getEle mentById('num1' ).childNodes[0].fileSize;
    alert("size = " + size_pic);
    which works on IE with one exception.

    With a first time displayed image, the script displays a size= -1 The image
    is displayed on the first pass. The fileSize script is located after the
    img script.
    var image_display=d ocument.createE lement('img');
    image_display.s rc='file://' + field;
    image_display.n ame = 'pict';
    document.getEle mentById('num1' ).appendChild(i mage_display);

    The second pass is correct. size = 11479

    Is this a cache problem? The first pass caches the file, the second pass
    reads the file?

    I tried running - var size_pic
    =document.getEl ementById('num1 ').childNodes[0].fileSize; - twice but that
    made no difference.

    Is there a work around for this problem.

    Is there a better way to measure filesize in JavaScript?

    Have a good day!

    Ken


  • Zifud

    #2
    Re: FileSize - image

    Ken wrote:[color=blue]
    > I would like to measure the fileSize of the image (without uploading it -[/color]

    You asked this question a couple of weeks ago, the answer is still the
    same - you can't. Read your thread of 9 Oct.

    Zif.

    Comment

    • Grant Wagner

      #3
      Re: FileSize - image

      Ken wrote:
      [color=blue]
      > I would like to measure the fileSize of the image (without uploading it -
      > php).
      >
      > I use:
      > var size_pic = document.getEle mentById('num1' ).childNodes[0].fileSize;
      > alert("size = " + size_pic);
      > which works on IE with one exception.
      >
      > With a first time displayed image, the script displays a size= -1 The image
      > is displayed on the first pass. The fileSize script is located after the
      > img script.
      > var image_display=d ocument.createE lement('img');
      > image_display.s rc='file://' + field;
      > image_display.n ame = 'pict';
      > document.getEle mentById('num1' ).appendChild(i mage_display);
      >
      > The second pass is correct. size = 11479
      >
      > Is this a cache problem? The first pass caches the file, the second pass
      > reads the file?[/color]

      No, the problem is that the file has not yet loaded by the time you attempt to
      access it's size. Make the display of it's size dependant on the onload event of
      the image.

      var image_display=d ocument.createE lement('img');
      image_display.o nload = function() {
      alert(this.file Size);
      }
      image_display.s rc='file://' + field;
      image_display.n ame = 'pict';
      document.getEle mentById('num1' ).appendChild(i mage_display);

      Of course, you'll need to replace alert(this.file Size) with something more
      useful, perhaps create another node under the image and display the size there.

      --
      Grant Wagner <gwagner@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq

      Comment

      • Ken

        #4
        Re: FileSize - image

        "Grant Wagner" <gwagner@agrico reunited.com> wrote in message
        news:41810E88.F C246091@agricor eunited.com...[color=blue]
        > Ken wrote:
        >[color=green]
        > > I would like to measure the fileSize of the image (without uploading[/color][/color]
        it -[color=blue][color=green]
        > > php).
        > >
        > > I use:
        > > var size_pic = document.getEle mentById('num1' ).childNodes[0].fileSize;
        > > alert("size = " + size_pic);
        > > which works on IE with one exception.
        > >
        > > With a first time displayed image, the script displays a size= -1 The[/color][/color]
        image[color=blue][color=green]
        > > is displayed on the first pass. The fileSize script is located after[/color][/color]
        the[color=blue][color=green]
        > > img script.
        > > var image_display=d ocument.createE lement('img');
        > > image_display.s rc='file://' + field;
        > > image_display.n ame = 'pict';
        > > document.getEle mentById('num1' ).appendChild(i mage_display);
        > >
        > > The second pass is correct. size = 11479
        > >
        > > Is this a cache problem? The first pass caches the file, the second[/color][/color]
        pass[color=blue][color=green]
        > > reads the file?[/color]
        >
        > No, the problem is that the file has not yet loaded by the time you[/color]
        attempt to[color=blue]
        > access it's size. Make the display of it's size dependant on the onload[/color]
        event of[color=blue]
        > the image.
        >
        > var image_display=d ocument.createE lement('img');
        > image_display.o nload = function() {
        > alert(this.file Size);
        > }
        > image_display.s rc='file://' + field;
        > image_display.n ame = 'pict';
        > document.getEle mentById('num1' ).appendChild(i mage_display);
        >
        > Of course, you'll need to replace alert(this.file Size) with something more
        > useful, perhaps create another node under the image and display the size[/color]
        there.[color=blue]
        >
        > --
        > Grant Wagner <gwagner@agrico reunited.com>
        > comp.lang.javas cript FAQ - http://jibbering.com/faq
        >[/color]
        Grant,
        Thanks for the suggestion.
        Works great.
        Ken




        Comment

        • Ken

          #5
          Re: FileSize - image

          "Grant Wagner" <gwagner@agrico reunited.com> wrote in message
          news:41810E88.F C246091@agricor eunited.com...[color=blue]
          > Ken wrote:
          >[color=green]
          > > I would like to measure the fileSize of the image (without uploading[/color][/color]
          it -[color=blue][color=green]
          > > php).
          > >
          > > I use:
          > > var size_pic = document.getEle mentById('num1' ).childNodes[0].fileSize;
          > > alert("size = " + size_pic);
          > > which works on IE with one exception.
          > >
          > > With a first time displayed image, the script displays a size= -1 The[/color][/color]
          image[color=blue][color=green]
          > > is displayed on the first pass. The fileSize script is located after[/color][/color]
          the[color=blue][color=green]
          > > img script.
          > > var image_display=d ocument.createE lement('img');
          > > image_display.s rc='file://' + field;
          > > image_display.n ame = 'pict';
          > > document.getEle mentById('num1' ).appendChild(i mage_display);
          > >
          > > The second pass is correct. size = 11479
          > >
          > > Is this a cache problem? The first pass caches the file, the second[/color][/color]
          pass[color=blue][color=green]
          > > reads the file?[/color]
          >
          > No, the problem is that the file has not yet loaded by the time you[/color]
          attempt to[color=blue]
          > access it's size. Make the display of it's size dependant on the onload[/color]
          event of[color=blue]
          > the image.
          >
          > var image_display=d ocument.createE lement('img');
          > image_display.o nload = function() {
          > alert(this.file Size);
          > }
          > image_display.s rc='file://' + field;
          > image_display.n ame = 'pict';
          > document.getEle mentById('num1' ).appendChild(i mage_display);
          >
          > Of course, you'll need to replace alert(this.file Size) with something more
          > useful, perhaps create another node under the image and display the size[/color]
          there.[color=blue]
          >
          > --
          > Grant Wagner <gwagner@agrico reunited.com>
          > comp.lang.javas cript FAQ - http://jibbering.com/faq
          >[/color]
          Grant,

          This works but not like I want.

          I am trying to determine the file size to test for size < max spec. The
          script works for the first image but not for a second image which replaces
          the first image if it is over spec.

          Removing the onload... the script works but will not measure fileSize on the
          first pass.

          Any suggestions.

          Ken

          <input type=file size=65 name="picture1" onChange="image _size(this.valu e);"
          Id="pt1">
          <div id="image_size_ display"></div>
          <script type="text/javascript">
          // Image size to be under max limit
          function image_size(fiel d){
          if ((document.crea teElement) && (document.getEl ementById)) {
          var image_display=d ocument.createE lement('img');
          image_display.o nload = function() { image_size = this.fileSize; }
          image_display.s rc='file://' + field;
          image_display.n ame = 'pict';
          image_display.a lt = 'image';

          document.getEle mentById('image _size_display') .appendChild(im age_display);

          alert("image_si ze = " + image_size);

          if(image_size >2000000){alert ("This picture ( file ) is geater than
          2,000,000.\n\n" + "Select another picture or reduce the size of the picture
          ( file )")};
          alert("test");

          document.getEle mentById('image _size_display') .removeChild(im age_display);
          } }
          </script>


          Comment

          Working...