日期:2010-08-09  浏览次数:21300 次

  关于图片上传的例子在网上有很多文章和原代码。但是每次上论坛浏览帖子的时候都是看到很多网友对图片上传感到头疼和麻烦。其实这个问题也是曾经让我感到头疼。也看过了不少的文章和代码。现在我写的这篇文章是把我比较喜欢的一篇代码和动网里的高手对这篇代码优化后再加上我增加一些代码结合出来的!呵呵,其实这篇文章是沾了写这篇代码的人(稻香居士)和动网里那些高手的光。:)

  好了,不说废话了。开始来搭建所需要的环境和数据结构!

  先新建一个名字叫photo的文件夹。(我在这里就是把图片上传到这个文件夹里的。)建立一个名字叫database的数据库。再接着建立一个名字叫sFile的表。表里设计四个字段分别是id(int),image(varchar),image1(varchar),image2(varchar)。我这里用的数据库是SQL。

  相关的文件

  register.asp

<html>
<head>
<title>文件</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<form name="form1" method="post" action="save.asp">
<table width="50%" border="1" cellspacing="0" cellpadding="0" align="center">
<tr> 
<td width="20%"><a href="#" >图片上传</a></td>
<td width="80%"> 
<input type="text" name="myface">
<input type="text" name="myface1">
<input type="text" name="myface2">
(此处用户不必填图片上传后自动生成) </td>
</tr>
<tr> 
<td colspan="2" align="center"> 
<input type="submit" name="Submit" value="提交">
<input type="reset" name="Submit2" value="重设">
</td>
</tr>
</table>
</form>
</body>
</html>

  reg_upload.asp

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body leftmargin="0" topmargin="0" >
  <br>
<br>
<br>
<table width="90%" border="0" align="center" bgcolor="#000000" height="152" cellspacing="1">
<tr> 
<td height="34" align="center" bgcolor="#FFFFFF"><font color="#FFFF33"><b><font size="4" color="#000000">选择图片</font></b></font></td>
</tr>
<tr> 
<td bgcolor="#FFFFFF"> 
<form name="form" method="post" action="upfile.asp" enctype="multipart/form-data" >
<input type="hidden" name="filepath" value="photo">
<input type="hidden" name="act" value="upload">
<input type="file" name="file1" size="30"><br>
<input type="file" name="file2" size="30"><br>
<input type="file" name="file3" size="30">

<input type="submit" name="Submit" value="粘 贴" class="tl">
</form>
</td>
</tr>
</table>
</body>
</html>

  upfile.asp

<!--#include FILE="upload.inc"-->
<html>
<head>
<title>文件上传</title>
</head>
<body>
<%
dim arr(3)
dim upload,file,formName,formPath,iCount,filename,fileExt,i
set upload=new upload_5xSoft ''''建立上传对象

formPath=upload.form("filepath")
''''在目录后加(/)
if right(formPath,1)<>"/" then formPath=formPath&"/" 
iCount=0
i=0
for each formName in upload.file ''''列出所有上传了的文件
set file=upload.file(formName) ''''生成一个文件对象

if file.filesize<0 then
response.write "<font size=2>请先选择你要上传的图片 [ <a href=# onclick=history.go(-1)>重新上传</a> ]</f