Checking Existence of File

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

    Checking Existence of File

    I had a form like below that validated that a file was there before it
    would submit.

    <form name="attach" method="POST" action="run_thi s_pgm.cfm"
    enctype="multip art/form-data" onSubmit="retur n(validateData( this))">
    <input type="file" name="txtFileTo Upload">
    <input type="submit" name="btnAdd" value="Add" class="form_but ton">
    </form>

    function checkFile(frm)
    {
    var strLength = frm.txtFileToUp load.value.leng th;
    var min = 5;
    if (strLength < min)
    {
    alert("Please choose a valid file to upload.");
    frm.txtFileToUp load.focus();
    frm.txtFileToUp load.select();
    return false;
    }
    }
    function validateData(fr m)
    {
    return (checkFile(frm) )
    }

    I wanted to change it to submit in a popup window instead and not open
    the new window unless a valid file exists. I made these changes below
    but it does not work. Somehow I need to check the existence of the file
    and not just that a string is in the textbox.

    <form name="attach" method="POST" action="run_thi s_pgm.cfm"
    enctype="multip art/form-data">
    <input type="file" name="txtFileTo Upload">
    <button name="btnAdd" onclick="save_a ttach(this.form );">Add</button>
    </form>

    function save_attach(frm )
    {
    if ( validateData(fr m) )
    {
    alert('should be ok');
    }
    }

    Any help is appreciated.

    Mike

  • Joakim Braun

    #2
    Re: Checking Existence of File

    "mike" <hillmw@charter .net> skrev i meddelandet
    news:1123611941 .109088.264300@ f14g2000cwb.goo glegroups.com.. .[color=blue]
    > I had a form like below that validated that a file was there before it
    > would submit.
    >
    > <form name="attach" method="POST" action="run_thi s_pgm.cfm"
    > enctype="multip art/form-data" onSubmit="retur n(validateData( this))">
    > <input type="file" name="txtFileTo Upload">
    > <input type="submit" name="btnAdd" value="Add" class="form_but ton">
    > </form>
    >
    > function checkFile(frm)
    > {
    > var strLength = frm.txtFileToUp load.value.leng th;
    > var min = 5;
    > if (strLength < min)
    > {
    > alert("Please choose a valid file to upload.");[/color]

    Why should a file whose name is less than 5 characters long be deemed
    invalid?
    Not all operating systems use file type extensions.
    [color=blue]
    > frm.txtFileToUp load.focus();
    > frm.txtFileToUp load.select();
    > return false;
    > }
    > }
    > function validateData(fr m)
    > {
    > return (checkFile(frm) )
    > }
    >
    > I wanted to change it to submit in a popup window instead and not open
    > the new window unless a valid file exists. I made these changes below
    > but it does not work. Somehow I need to check the existence of the file
    > and not just that a string is in the textbox.[/color]

    The existence and integrity of the file is most reliably checked
    server-side. And you need to do the checking anyhow. If the file did not
    exist, no problem - you'll be able to find out very quickly server-side, and
    returning an error message to the user should be quick and simple.

    A scripting system that granted arbitrary "does a file by this name exist
    somewhere" access to the user's local hard drive could tell you quite a lot
    about the state of the user's system.
    [color=blue]
    > <form name="attach" method="POST" action="run_thi s_pgm.cfm"
    > enctype="multip art/form-data">
    > <input type="file" name="txtFileTo Upload">
    > <button name="btnAdd" onclick="save_a ttach(this.form );">Add</button>
    > </form>
    >
    > function save_attach(frm )
    > {
    > if ( validateData(fr m) )
    > {
    > alert('should be ok');
    > }
    > }[/color]

    You may be able to do this with nonstandard IE-only technologies. Watch for
    more replies.

    --
    Joakim Braun


    Comment

    • Danny

      #3
      Re: Checking Existence of File



      The type="file" input element is one that gives little access for
      Security hazards, fairly you won't get many of those invalid calls, but
      the checks you want to do, as indicated already, are done on the server,
      on the server you do have access to the filesize and other properties of
      the object, on the client, you do not due to security issues. And yes, in
      IE you could by using an activeXObject but then again, that'll work only
      for IE in Win32 only.

      Danny

      --
      Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

      Comment

      Working...