By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,563 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,563 IT Pros & Developers. It's quick & easy.

Checking Existence of File

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
"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

P: n/a


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.