打印本文 打印本文  关闭窗口 关闭窗口
多图片上传到指定的目录并存到数据库
作者:采集员 文章来源:来源于网络 点击数: 更新时间:2005/9/10 13:19:37
关于图片上传的例子在网上有很多文章和原代码。但是每次上论坛浏览帖子的时候都是看到很多网友对图片上传感到头疼和麻烦。其实这个问题也是曾经让我感到头疼。也看过了不少的文章和代码。现在我写的这篇文章是把我比较喜欢的一篇代码和动网里的高手对这篇代码优化后再加上我增加一些代码结合出来的!呵呵,其实这篇文章是沾了写这篇代码的人(稻香居士)和动网里那些高手的光。:)
好了,不说废话了。开始来搭建所需要的环境和数据结构!
先新建一个名字叫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="#"  OnClick="javascript:window.open('/reg_upload.asp',null,'    left=40%,top=40%,height=250,width=470,status=yes,toolbar=no,menubar=no,resizable=yes,copyhistory=yes,scrollbars=yes,location=no,status=no,titlebar=no')">图片上传</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> ]</font>"
        response.end
    end if
   
    if file.filesize>100000 then
        response.write "<font size=2>图片大小超过了限制 [ <a href=# onclick=history.go(-1)>重新上传</a> ]</font>"
        response.end
    end if
if fileEXT<>".gif" and fileEXT<>".jpg" then
     response.write "<font size=2>文件格式不对 [ <a href=# onclick=history.go(-1)>重新上传</a> ]</font>"
    response.end
end if

    fileExt=lcase(right(file.filename,4))
    filename=formPath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&file.FileName
    arr(i)=filename
    i=i+1

    if file.FileSize>0 then         '如果 FileSize > 0 说明有文件数据
        file.SaveAs Server.mappath(filename)   '保存文件
        ' response.write file.FilePath&file.FileName&" ("&file.FileSize&") => "&formPath&File.FileName&" 成功!<br>"

          iCount=iCount+1
    end if
    set file=nothing
next
if instr(arr(0),".")=0 and instr(arr(1),".")=0  and instr(arr(2),".")=0  then
  response.write "<font size=2>请先选择你要上传的图片 [ <a href=# onclick=history.go(-1)>重新上传</a> ]</font>"
response.end
end if
if instr(arr(0),".")<>0 then
response.write "<script>opener.document.forms[0].myface.value='" & arr(0) & "'</script>"
else
response.write "<script>opener.document.forms[0].myface.value='</script>"
end if
if instr(arr(1),".")<>0 then
response.write "<script>opener.document.forms[0].myface1.value='" & arr(1) & "'</script>"
else
response.write "<script>opener.document.forms[0].myface1.value='</script>"
end if
if instr(arr(2),".")<>0 then
response.write "<script>opener.document.forms[0].myface2.value='" & arr(2) & "'</script>"
else
response.write "<script>opener.document.forms[0].myface2.value=' </script>"
end if
set upload=nothing  '删除此对象

session("upface")="done"

Htmend iCount&" 个文件上传结束!"

sub HtmEnd(Msg)
    set upload=nothing
response.write "<html><head><meta  http-equiv='Refresh' content='3 url=""javascript:window.close();""'></head><body><center><br><br>文件上传成功<br>谢谢你的支持!<br>本窗口三秒后自动关闭</center></body></html>"
   response.end
end sub
%>
</body>
</html>
upload.inc
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>

dim upfile_5xSoft_Stream

Class upload_5xSoft
  
dim Form,File,Version
  
Private Sub Class_Initialize
        dim iStart,iFileNameStart,iFileNameEnd,iEnd,vbEnter,iFormStart,iFormEnd,theFile
        dim strDiv,mFormName,mFormValue,mFileName,mFileSize,mFilePath,iDivLen,mStr
        Version=""
        if Request.TotalBytes<1 then Exit Sub
        set Form=CreateObject("Scripting.Dictionary")
        set File=CreateObject("Scripting.Dic

[1] [2] [3]  下一页



打印本文 打印本文  关闭窗口 关闭窗口