469,572 Members | 1,281 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

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_this_pgm.cfm"
enctype="multipart/form-data" onSubmit="return(validateData(this))">
<input type="file" name="txtFileToUpload">
<input type="submit" name="btnAdd" value="Add" class="form_button">
</form>

function checkFile(frm)
{
var strLength = frm.txtFileToUpload.value.length;
var min = 5;
if (strLength < min)
{
alert("Please choose a valid file to upload.");
frm.txtFileToUpload.focus();
frm.txtFileToUpload.select();
return false;
}
}
function validateData(frm)
{
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_this_pgm.cfm"
enctype="multipart/form-data">
<input type="file" name="txtFileToUpload">
<button name="btnAdd" onclick="save_attach(this.form);">Add</button>
</form>

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

Any help is appreciated.

Mike

Aug 9 '05 #1
2 1815
"mike" <hi****@charter.net> skrev i meddelandet
news:11**********************@f14g2000cwb.googlegr oups.com...
I had a form like below that validated that a file was there before it
would submit.

<form name="attach" method="POST" action="run_this_pgm.cfm"
enctype="multipart/form-data" onSubmit="return(validateData(this))">
<input type="file" name="txtFileToUpload">
<input type="submit" name="btnAdd" value="Add" class="form_button">
</form>

function checkFile(frm)
{
var strLength = frm.txtFileToUpload.value.length;
var min = 5;
if (strLength < min)
{
alert("Please choose a valid file to upload.");
Why should a file whose name is less than 5 characters long be deemed
invalid?
Not all operating systems use file type extensions.
frm.txtFileToUpload.focus();
frm.txtFileToUpload.select();
return false;
}
}
function validateData(frm)
{
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.
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.
<form name="attach" method="POST" action="run_this_pgm.cfm"
enctype="multipart/form-data">
<input type="file" name="txtFileToUpload">
<button name="btnAdd" onclick="save_attach(this.form);">Add</button>
</form>

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


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

--
Joakim Braun
Aug 9 '05 #2


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/
Aug 9 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by GujuBoy | last post: by
14 posts views Thread by Leslaw Bieniasz | last post: by
1 post views Thread by Xeno Campanoli | last post: by
5 posts views Thread by Richard L Rosenheim | last post: by
15 posts views Thread by Geiregat Jonas | last post: by
2 posts views Thread by John Smith | last post: by
2 posts views Thread by Anoop | last post: by
4 posts views Thread by Patient Guy | last post: by
26 posts views Thread by Army1987 | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.