File Size

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

    File Size

    Can JavaScript detect the size of an image file?

    Ken



  • Andrew Thompson

    #2
    Re: File Size

    On Mon, 6 Sep 2004 15:39:18 -0500, Ken wrote:
    [color=blue]
    > Can JavaScript detect the size of an image file?[/color]

    I do not know if there are other ways to do it, but
    ...does your server return file listings like this?
    <http://www.physci.org/test/screenshot/>

    That is a directory on my server that contains
    no 'index' page, so the server writes a file list
    ...complete with file size.

    Assuming you can access the elements using JS,
    you might be able to load that page in a hidden
    or IFRAME and parse it for the information.

    Be warned though, that I am merely lurking here
    trying to pick up tips from the experts. ;-)

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology

    Comment

    • Grant Wagner

      #3
      Re: File Size

      Ken wrote:
      [color=blue]
      > Can JavaScript detect the size of an image file?
      >
      > Ken[/color]

      No, JavaScript can not. However, at least one user agent has a
      host Image object which includes a property that lets you access
      the information you require. JavaScript can then be used to
      access that host object property:

      <url:
      Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

      />

      <body onload="alert(d ocument.images['myImage'].fileSize);">
      <img name="myImage" src="myImage.gi f">
      </body>

      Note that of IE 6SP1, Firefox 0.9.3, Mozilla 1.7.2, Opera 7.54,
      Opera 6.05 and Netscape 4.78, only IE does not report "undefined"
      in the above code example.

      The other possibility would be to use the XML HTTP Request object
      available in IE and Gecko-based browsers to do a HEAD request for
      the image. The headers that get returned should contain the image
      size (Content-Length) <url:
      http://jibbering.com/2002/4/httprequest.html />.

      Lastly, if you have any sort of server-side technology available
      to you (ASP, JSP, Perl, PHP, etc), you could use it to retrieve
      the size of the image and then "pass" that value to the
      client-side JavaScript:

      <?php
      # use PHP to determine the size of the image
      ?>
      <script type="text/javascript">
      var imageSize = <?php echo $imageSize ?>;
      </script>

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

      Comment

      Working...