主题:  上载文件by perl || JS(范例)

QQlan

职务:版主
等级:1
金币:1.0
发贴:158
注册:2000/9/19 10:35:21
#12001/5/25 6:09:20
我记得从前有人问过怎么在网页上上载本地文件:

今天在这里提供两种方法的源代码:

首先推荐 PERL4 语言写的代码: (我会增加一个perl 5 的贴子在最后以供大家选择使用)
--------------------------------------------------------------------------------------------------

#!/usr/local/bin/perl

# Copyright (c) 1996 Steven E. Brenner
# $Id: fup.cgi,v 1.2 1996/03/30 01:35:32 brenner Exp $


require "./cgi-lib.pl";

# When writing files, several options can be set... here we just set one
# Limit upload size to avoid using too much memory
$cgi_lib'maxdata = 50000;


# Start off by reading and parsing the data. Save the return value.
# We could also save the file's name and content type, but we don't
# do that in this example.
$ret = &ReadParse;


# A bit of error checking never hurt anyone
&CgiDie("Error in reading and parsing of CGI input") if !defined $ret;
&CgiDie("No data uploaded",
    "Please enter it in fup.html.") if !$ret;


# Munge the uploaded text so that it doesn't contain HTML elements
# This munging isn't complete -- lots of illegal characters are left as-is.
# However, it takes care of the most common culprits.
$in{'upfile'} =~ s/$in{'upfile'} =~ s/>/>/g;


# Now produce the result: an HTML page...
print &PrintHeader;
print &HtmlTop("File Upload Results");
print <

You've uploaded a file. Your notes on the file were:

$in{'note'}


The file's contents are:


$in{'upfile'}

EOT

print &HtmlBot;


-------------------------------------------------------------------------------------------------------------------------------

编辑历史:[这消息被QQLan编辑过(编辑时间2001-05-25 06:15:57)]


QQlan

职务:版主
等级:1
金币:1.0
发贴:158
注册:2000/9/19 10:35:21
#22001/5/25 6:12:26
第二种方法是由javascript 实现:

以下贴在 部分---------------------------------------------------------------------------------------------







以下贴在 部分--------------------------------------------------------------------------------------



Please upload only images that end in:













QQlan

职务:版主
等级:1
金币:1.0
发贴:158
注册:2000/9/19 10:35:21
#32001/5/25 6:18:57
有兴趣的朋友还可以研究一下perl 5写的 upload files :

----------------------------------------------------------------------------------------
#!/usr/local/bin/perl -Tw

# Copyright (c) 1996 Steven E. Brenner
# $Id: fup.cgi,v 1.2 1996/03/30 01:33:46 brenner Exp $

require 5.001;
use strict;
require "./cgi-lib.pl";

MAIN:
{
my (%cgi_data, # The form data
%cgi_cfn, # The uploaded file(s) client-provided name(s)
%cgi_ct, # The uploaded file(s) content-type(s). These are
# set by the user's browser and may be unreliable
%cgi_sfn, # The uploaded file(s) name(s) on the server (this machine)
$ret, # Return value of the ReadParse call.
$buf # Buffer for data read from disk.
);

# When writing files, several options can be set..
# Spool the files to the /tmp directory
$cgi_lib::writefiles = "/tmp";

# Limit upload size to avoid using too much memory
$cgi_lib::maxdata = 50000;

# Start off by reading and parsing the data. Save the return value.
# Pass references to retreive the data, the filenames, and the content-type
$ret = &ReadParse(\%cgi_data,\%cgi_cfn,\%cgi_ct,\%cgi_sfn);

# A bit of error checking never hurt anyone
if (!defined $ret) {
&CgiDie("Error in reading and parsing of CGI input");
} elsif (!$ret) {
&CgiDie("Missing parameters\n",
     "Please complete the form fup.html.\n");
} elsif (!defined $cgi_data{'upfile'} or !defined $cgi_data{'note'}) {
&CgiDie("Data missing\n",
     "Please complete the form fup.html.\n");
}



# Now print the page for the user to see...
print &PrintHeader;
print &HtmlTop("File Upload Results");

print <

You've uploaded a file. Your notes on the file were:

$cgi_data{'note'}


The file has been spooled to disk as: $cgi_data{'upfile'}

The file's reported name on the client machine is:
$cgi_cfn{'upfile'}

The file's reported Content-type (possibly none) was:
$cgi_ct{'upfile'}



The contents of $cgi_data{'upfile'} are as follows:


EOT

# Print the contents of the uploaded file
open (UPFILE, $cgi_sfn{'upfile'}) or
&CgiError("Error: Unable to open file $cgi_sfn{'upfile'}: $!\n");
$buf = ""; # avoid annoying warning message
while (read (UPFILE, $buf, 8192)) {
# Munge the uploaded text so that it doesn't contain HTML elements
# This munging isn't complete -- lots of illegal characters are left
# However, it takes care of the most common culprits.
$buf =~ s/ $buf =~ s/>/>/g;
print $buf;
}
close (UPFILE);

print "
\n";

unlink ($cgi_sfn{'upfile'}) or
&CgiError("Error: Unable to delete file",
"Error: Unable to delete file $cgi_sfn{'upfile'}: $!\n");
# cleanup - delete the uploaded file
# Note that when using spooling of files to disk, the uploaded file's
# name on the server machine is in both %cgi_data and %cgi_sfn
# (that is, the first and fourth parameters to ReadParse). However,
# for technical reasons, the data in %cgi_data are tainted. The data in
# %cgi_sfn are not tainted, but the keys can contain only a limited
# set of characters ([-\w] in cgi-lib 2.8)

print "
File $cgi_data{'upfile'} has now been removed\n";
print &HtmlBot;


# The following lines are solely to suppress 'only used once' warnings
$cgi_lib::writefiles = $cgi_lib::writefiles;
$cgi_lib::maxdata = $cgi_lib::maxdata;

}

----------------------------------------------------------------------------------------------------



5D君子

职务:普通成员
等级:4
金币:10.0
发贴:1453
注册:2001/2/22 13:43:15
#42001/5/28 13:29:29
QQLan在上个贴子中说
引用:
第二种方法是由javascript 实现:

以下贴在 部分---------------------------------------------------------------------------------------------







以下贴在 部分--------------------------------------------------------------------------------------



Please upload only images that end in:













这个是验证是不是图形文件的代码吧!还得有后的代码吧!



QQlan

职务:版主
等级:1
金币:1.0
发贴:158
注册:2000/9/19 10:35:21
#52001/5/31 4:00:03
Heman在上个贴子中说
引用:
这个是验证是不是图形文件的代码吧!还得有后的代码吧! :confused:



en? 你讲的什么意思? 没看懂你的意思 8?

编辑历史:[这消息被QQLan编辑过(编辑时间2001-05-31 04:00:43)]


5D君子

职务:普通成员
等级:4
金币:10.0
发贴:1453
注册:2001/2/22 13:43:15
#62001/5/31 11:19:39
我说这只是一段验证接收的程序!而不是图片上传的程序!javascript能做后台吗?