File size...

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

    File size...

    I have developed an upload tool, that allows users to upload files (funny
    that). The asp form object has a 'SizeLimit' property that can be set, to
    prevent files being uploaded, that are too large. Is there a way in
    Javascript, to interrogate the file size before the form is posted, so that
    the validation can be done before trying to post the form.

    The only information I can find, on file size, relates to images. But any
    file can be uploaded through this tool, so I also need to determine the size
    of other binary and text files.


  • Erwin Moller

    #2
    Re: File size...

    Ruskin wrote:
    [color=blue]
    > I have developed an upload tool, that allows users to upload files (funny
    > that). The asp form object has a 'SizeLimit' property that can be set, to
    > prevent files being uploaded, that are too large. Is there a way in
    > Javascript, to interrogate the file size before the form is posted, so
    > that the validation can be done before trying to post the form.
    >
    > The only information I can find, on file size, relates to images. But any
    > file can be uploaded through this tool, so I also need to determine the
    > size of other binary and text files.[/color]

    Hi,

    As far as I know: No. You cannot.

    Javascript is intentionally crippled when it come to local file, for obvious
    securityreasons .

    Maybe clientside VB-script can: VB is well known for such 'features'.
    I am unsure.
    Anyway, that would limit your your clients to browsers that support
    VB-script, not a wise choice. :-(

    Regards,
    Erwin Moller

    Comment

    • Grant Wagner

      #3
      Re: File size...

      Erwin Moller wrote:
      [color=blue]
      > Javascript is intentionally crippled when it come to local file, for obvious
      > securityreasons .[/color]

      No, the browser DOM is intentionally crippled when it comes to local files.
      JavaScript itself has no way of accessing files at all.

      This is more than just nitpicking. JavaScript itself can contain security
      vulnerabilities , for example I vaguely remember some Array or String overflow
      vulnerability in early versions of JavaScript, where a specially constructed
      Array or String could result in code execution.

      But the more importantly, the DOM that JavaScript (or VBScript or PerlScript or
      AnyScript) accesses can contain security vulnerabilities , and that is where most
      of the problems appear.
      [color=blue]
      > Maybe clientside VB-script can: VB is well known for such 'features'.
      > I am unsure.[/color]

      There is no difference between the things that can be done in Internet Explorer
      between JavaScript and VBScript (well, there are differences, but not in terms
      of the security implications you are referring to). In other words, the security
      vulnerabilities are in the DOM and the ActiveX components, not really in the
      scripting language itself.

      If you can instanciate a FileSystemObjec t in VBScript and manipulate local
      files, you can instanciate a FileSystemObjec t in JavaScript and manipulate local
      files.
      [color=blue]
      > Anyway, that would limit your your clients to browsers that support
      > VB-script, not a wise choice. :-([/color]

      The choice would be between a user agent that support an object with properties
      and methods capable of accessing the local file system, and using a user agent
      that does not.

      However, a solution to his question has already been provided (at least in
      Internet Explorer):

      <form name="myForm">
      <input type="file" name="myFile">
      <script type="text/javascript">
      if ((new Image()).fileSi ze) {
      document.write(
      '<input type="button"' +
      ' name="mySize" value="Show Size"' +
      ' onclick="showSi ze(this.form);" >'
      );
      }
      function showSize(f) {
      if ((new Image()).fileSi ze) {
      var img = new Image();
      img.onload = imgOnload;
      img.onerror = imgOnerror;
      img.src = f.elements['myFile'].value;
      }
      }
      function imgOnload() {
      if (this.fileSize) {
      var size = this.fileSize;
      if (size > 1024) {
      size = Math.round((siz e / 1024) * 100) / 100;
      }
      alert(
      'The file is ' + size +
      (this.fileSize > 1024 ? ' kilobytes' : ' bytes') +
      ' in size.'
      );
      }
      }
      function imgOnerror() {
      alert('The file is not an image or did not load.');
      }
      </script>
      </form>

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

      Comment

      Working...