Checking Existence of File

M

mike

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
 
J

Joakim Braun

mike said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top