PDA

View Full Version : Choose file on form and submit ???? Please Help


Rich
07-20-2002, 06:31 PM
Hi,

I put a java script in a form which allows the user to choose a file to send with the rest of the form answers. JUST LIKE THE ONE IN THE "POST NEW THREAD" PAGE ON THIS SITE. This is the script below. It browses for the file and it also lets the user view the file too, but I do not receive it in the email with the rest of the info. Should this be a CGI script I am looking for instead of a java script to let the user browse and also submit? Or, maybe it's the wrong script. If not, I can post the rest of the HTML from that page later. Thanks.

Rich


<HEAD>

<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Hoop Hooper (hooper119@aol.com) -->
<!-- Web Site: http://www.angelfire.com/pa3/muddas -->
<!-- Modified: Larry McClurg (larry@honeybells.com) -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
// Thank you Larry McClurg for the cross-browser fix
function whatFile() {
window.location= 'file:///' + document.form1.cmuds.value;
}
// End -->
</script>

</HEAD>

<BODY>

<form name=form1>
<input type=file name="cmuds">
<input type=button onClick="whatFile()" value="Open File">
</form>

<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

<!-- Script Size: 1.09 KB -->:)

Mouldy_Goat
07-20-2002, 09:54 PM
That's not what you want really if you want to allow users to upload files, that simply allows users to view the files themselves.

Uploading files is definitely a server-side task, and there are plenty of scripts at various free script resources which will be able to help you with this, such as www.hotscripts.com and cgi.resourceindex.com,

Hope that helps..

crca
07-29-2002, 10:24 AM
#
# Perl-based Upload Script
#
# Copyright 1998 Tod Sambar
# All rights reserved.
#
# Demonstrates how to upload data via multipart/form-data.
#


#
# PARSE THE CGI FORM
#
$content_type = $ENV{'CONTENT_TYPE'};
$content_len = $ENV{'CONTENT_LENGTH'};
$host_test = $ENV{'REMOTE_ADDR'};
# Buffer the POST content
binmode STDIN;
read(STDIN, $buffer, $content_len);

if ((!$content_type) ||
($content_type eq 'application/x-www-form-urlencoded'))
{
# Process the name=value argument pairs
@args = split(/&/, $buffer);

$data = '';
foreach $pair (@args)
{
($name, $value) = split(/=/, $pair);

# Unescape the argument value
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

# Save the name=value pair for use below.
$FORM{$name} = $value;
}
}
elsif ($content_type =~ m#^multipart/form-data#)
{
# find boundary
# Eric Poulsen fixed the following to allow for quotes.
#
# ($boundary = $content_type) =~ s/^.*boundary=(.*)$/\1/;
($boundary = $content_type) =~ s/^.*boundary="?(.*?)"?$/\1/;

@pairs = split(/--$boundary/, $buffer);
@pairs = splice(@pairs,1,$#pairs-1);

for $part (@pairs)
{
($dump,$fline,$value) = split(/\r\n/,$part,3);
next if $fline =~ /filename=\"\"/;
$fline =~ s/^Content-Disposition: form-data; //;
(@columns) = split(/;\s+/, $fline);
($name = $columns[0]) =~ s/^name="([^"]+)"$/\1/g;

if ($#columns > 0)
{
if ($value =~ /^Content-Type:/)
{
($dump,$dump,$value) = split(/\r\n/,$value,3);
}
else
{
($dump,$value) = split(/\r\n/,$value,2);
}
}
else
{
($dump,$value) = split(/\r\n/,$value,2);
if (grep(/^$name$/, keys(%CGI)))
{
if (@{$FORM{$name}} > 0)
{
push(@{$FORM{$name}}, $value);
}
else
{
$arrvalue = $FORM{$name};
undef $FORM{$name};
$FORM{$name}[0] = $arrvalue;
push(@{$FORM{$name}}, $value);
}
}
else
{
next if $value =~ /^\s*$/;
$FORM{$name} = $value;
}
next;
}

$FORM{$name} = $value;
}
}
else
{
print "Invalid content type!\n";
exit(1);
}

#
# VERIFY THE FORM DATA
#
$upfile = $FORM{'upfile'};
$upname = $FORM{'upname'};
if (!($upfile) || !($upname))
{
print "<HTML><TITLE>Missing fields</TITLE><BODY>\n";
print "No upload file specified!\n";
print "</BODY></HTML>\n";
exit(1);
}


#
# CLOSE SECURITY PROBLEMS.
#
if ($upname =~ /[;><&\*'\|\/\\]/ )
{
print "<HTML><TITLE>Invalid file name</TITLE><BODY>\n";
print "The upload file name is invalid.\n";
print "</BODY></HTML>\n";
exit(1);
}

#
# Write out the upload file
#
$filename = "../docs/upload/".$upname;
open(FILE, ">$filename") || exit(1);
binmode FILE;

print FILE $upfile;
close FILE;


#
# DONE
#
print "Upload of ".$upname." succeeded.\n";

exit(0);