多图片上传到指定的目录并存到数据库

关于图片上传的例子在网上有很多文章和原代码。但是每次上论坛浏览帖子的时候都是看到很多网友对图片上传感到头疼和麻烦。其实这个问题也是曾经让我感到头疼。也看过了不少的文章和代码。现在我写的这篇文章是把我比较喜欢的一篇代码和动网里的高手对这篇代码优化后再加上我增加一些代码结合出来的!呵呵,其实这篇文章是沾了写这篇代码的人(稻香居士)和动网里那些高手的光。:)
好了,不说废话了。开始来搭建所需要的环境和数据结构!
先新建一个名字叫photo的文件夹。(我在这里就是把图片上传到这个文件夹里的。)建立一个名字叫database的数据库。再接着建立一个名字叫sFile的表。表里设计四个字段分别是id(int),image(varchar),image1(varchar),image2(varchar)。我这里用的数据库是SQL。
相关的文件
register.asp

 1<html>
 2<head>
 3<title>文件</title>
 4<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
 5</head>
 6<body>
 7<form action="save.asp" method="post" name="form1">
 8<table align="center" border="1" cellpadding="0" cellspacing="0" width="50%">
 9<tr>
10<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>
11<td width="80%">
12<input name="myface" type="text"/>
13<input name="myface1" type="text"/>
14<input name="myface2" type="text"/>   
15(此处用户不必填图片上传后自动生成) </td>
16</tr>
17<tr>
18<td align="center" colspan="2">
19<input name="Submit" type="submit" value="提交"/>
20<input name="Submit2" type="reset" value="重设"/>
21</td>
22</tr>
23</table>
24</form>
25</body>
26</html>

reg_upload.asp

 1<html>
 2<head>
 3<title></title>
 4<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
 5<link href="css/style.css" rel="stylesheet" type="text/css"/>
 6</head>
 7<body leftmargin="0" topmargin="0">
 8<br/>
 9<br/>
10<br/>
11<table align="center" bgcolor="#000000" border="0" cellspacing="1" height="152" width="90%">
12<tr>
13<td align="center" bgcolor="#FFFFFF" height="34"><font color="#FFFF33"><b><font color="#000000" size="4">选择图片</font></b></font></td>
14</tr>
15<tr>
16<td bgcolor="#FFFFFF">
17<form action="upfile.asp" enctype="multipart/form-data" method="post" name="form">
18<input name="filepath" type="hidden" value="photo"/>
19<input name="act" type="hidden" value="upload"/>
20<input name="file1" size="30" type="file"/><br/>
21<input name="file2" size="30" type="file"/><br/>
22<input name="file3" size="30" type="file"/>
23<input class="tl" name="Submit" type="submit" value="粘 贴"/>
24</form>
25</td>
26</tr>
27</table>
28</body>
29</html>

upfile.asp

1<html>
2<head>
3<title>文件上传</title>
4</head>
5<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)&lt;&gt;"/" then formPath=formPath&amp;"/"
iCount=0
i=0
for each formName in upload.file ''列出所有上传了的文件
set file=upload.file(formName) ''生成一个文件对象

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

if file.filesize&gt;100000 then
response.write "<font size="2">图片大小超过了限制 [ <a href="#" onclick="history.go(-1)">重新上传</a> ]</font>"
response.end
end if
if fileEXT&lt;&gt;".gif" and fileEXT&lt;&gt;".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&amp;year(now)&amp;month(now)&amp;day(now)&amp;hour(now)&amp;minute(now)&amp;second(now)&amp;file.FileName
arr(i)=filename
i=i+1

if file.FileSize&gt;0 then ''如果 FileSize &gt; 0 说明有文件数据
file.SaveAs Server.mappath(filename) ''保存文件
' response.write file.FilePath&amp;file.FileName&amp;" ("&amp;file.FileSize&amp;") =&gt; "&amp;formPath&amp;File.FileName&amp;" 成功!<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),".")&lt;&gt;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),".")&lt;&gt;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),".")&lt;&gt;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&amp;" 个文件上传结束!"

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

1</body>
2</html>

upload.inc

  1<script language="VBSCRIPT" runat="SERVER">   
  2  
  3dim upfile_5xSoft_Stream   
  4  
  5Class upload_5xSoft   
  6  
  7dim Form,File,Version   
  8  
  9Private Sub Class_Initialize   
 10dim iStart,iFileNameStart,iFileNameEnd,iEnd,vbEnter,iFormStart,iFormEnd,theFile   
 11dim strDiv,mFormName,mFormValue,mFileName,mFileSize,mFilePath,iDivLen,mStr   
 12Version=""   
 13if Request.TotalBytes<1 then Exit Sub   
 14set Form=CreateObject("Scripting.Dictionary")   
 15set File=CreateObject("Scripting.Dictionary")   
 16set upfile_5xSoft_Stream=CreateObject("Adodb.Stream")   
 17upfile_5xSoft_Stream.mode=3   
 18upfile_5xSoft_Stream.type=1   
 19upfile_5xSoft_Stream.open   
 20upfile_5xSoft_Stream.write Request.BinaryRead(Request.TotalBytes)   
 21  
 22vbEnter=Chr(13)&Chr(10)   
 23iDivLen=inString(1,vbEnter)+1   
 24strDiv=subString(1,iDivLen)   
 25iFormStart=iDivLen   
 26iFormEnd=inString(iformStart,strDiv)-1   
 27while iFormStart < iFormEnd   
 28iStart=inString(iFormStart,"name=""")   
 29iEnd=inString(iStart+6,"""")   
 30mFormName=subString(iStart+6,iEnd-iStart-6)   
 31iFileNameStart=inString(iEnd+1,"filename=""")   
 32if iFileNameStart>0 and iFileNameStart<iFormEnd then   
 33iFileNameEnd=inString(iFileNameStart+10,"""")   
 34mFileName=subString(iFileNameStart+10,iFileNameEnd-iFileNameStart-10)   
 35iStart=inString(iFileNameEnd+1,vbEnter&vbEnter)   
 36iEnd=inString(iStart+4,vbEnter&strDiv)   
 37if iEnd>iStart then   
 38mFileSize=iEnd-iStart-4   
 39else   
 40mFileSize=0   
 41end if   
 42set theFile=new FileInfo   
 43theFile.FileName=getFileName(mFileName)   
 44theFile.FilePath=getFilePath(mFileName)   
 45theFile.FileSize=mFileSize   
 46theFile.FileStart=iStart+4   
 47theFile.FormName=FormName   
 48file.add mFormName,theFile   
 49else   
 50iStart=inString(iEnd+1,vbEnter&vbEnter)   
 51iEnd=inString(iStart+4,vbEnter&strDiv)   
 52  
 53if iEnd>iStart then   
 54mFormValue=subString(iStart+4,iEnd-iStart-4)   
 55else   
 56mFormValue=""   
 57end if   
 58form.Add mFormName,mFormValue   
 59end if   
 60  
 61iFormStart=iformEnd+iDivLen   
 62iFormEnd=inString(iformStart,strDiv)-1   
 63wend   
 64End Sub   
 65  
 66Private Function subString(theStart,theLen)   
 67dim i,c,stemp   
 68upfile_5xSoft_Stream.Position=theStart-1   
 69stemp=""   
 70for i=1 to theLen   
 71if upfile_5xSoft_Stream.EOS then Exit for   
 72c=ascB(upfile_5xSoft_Stream.Read(1))   
 73If c > 127 Then   
 74if upfile_5xSoft_Stream.EOS then Exit for   
 75stemp=stemp&Chr(AscW(ChrB(AscB(upfile_5xSoft_Stream.Read(1)))&ChrB(c)))   
 76i=i+1   
 77else   
 78stemp=stemp&Chr(c)   
 79End If   
 80Next   
 81subString=stemp   
 82End function   
 83  
 84Private Function inString(theStart,varStr)   
 85dim i,j,bt,theLen,str   
 86InString=0   
 87Str=toByte(varStr)   
 88theLen=LenB(Str)   
 89for i=theStart to upfile_5xSoft_Stream.Size-theLen   
 90if i>upfile_5xSoft_Stream.size then exit Function   
 91upfile_5xSoft_Stream.Position=i-1   
 92if AscB(upfile_5xSoft_Stream.Read(1))=AscB(midB(Str,1)) then   
 93InString=i   
 94for j=2 to theLen   
 95if upfile_5xSoft_Stream.EOS then   
 96inString=0   
 97Exit for   
 98end if   
 99if AscB(upfile_5xSoft_Stream.Read(1))<>AscB(MidB(Str,j,1)) then   
100InString=0   
101Exit For   
102end if   
103next   
104if InString<>0 then Exit Function   
105end if   
106next   
107End Function   
108  
109Private Sub Class_Terminate   
110form.RemoveAll   
111file.RemoveAll   
112set form=nothing   
113set file=nothing   
114upfile_5xSoft_Stream.close   
115set upfile_5xSoft_Stream=nothing   
116End Sub   
117  
118  
119Private function GetFilePath(FullPath)   
120If FullPath <> "" Then   
121GetFilePath = left(FullPath,InStrRev(FullPath, "\"))   
122Else   
123GetFilePath = ""   
124End If   
125End function   
126  
127Private function GetFileName(FullPath)   
128If FullPath <> "" Then   
129GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)   
130Else   
131GetFileName = ""   
132End If   
133End function   
134  
135Private function toByte(Str)   
136dim i,iCode,c,iLow,iHigh   
137toByte=""   
138For i=1 To Len(Str)   
139c=mid(Str,i,1)   
140iCode =Asc(c)   
141If iCode<0 Then iCode = iCode + 65535   
142If iCode>255 Then   
143iLow = Left(Hex(Asc(c)),2)   
144iHigh =Right(Hex(Asc(c)),2)   
145toByte = toByte & chrB("&H"&iLow) & chrB("&H"&iHigh)   
146Else   
147toByte = toByte & chrB(AscB(c))   
148End If   
149Next   
150End function   
151End Class   
152  
153  
154Class FileInfo   
155dim FormName,FileName,FilePath,FileSize,FileStart   
156Private Sub Class_Initialize   
157FileName = ""   
158FilePath = ""   
159FileSize = 0   
160FileStart= 0   
161FormName = ""   
162End Sub   
163  
164Public function SaveAs(FullPath)   
165dim dr,ErrorChar,i   
166SaveAs=1   
167if trim(fullpath)="" or FileSize=0 or FileStart=0 or FileName="" then exit function   
168if FileStart=0 or right(fullpath,1)="/" then exit function   
169set dr=CreateObject("Adodb.Stream")   
170dr.Mode=3   
171dr.Type=1   
172dr.Open   
173upfile_5xSoft_Stream.position=FileStart-1   
174upfile_5xSoft_Stream.copyto dr,FileSize   
175dr.SaveToFile FullPath,2   
176dr.Close   
177set dr=nothing   
178SaveAs=0   
179end function   
180End Class   
181</script>

save.asp

1 dim dsn,conn   
2dsn="driver={sql server};server=127.0.0.1;uid=sa;pwd=;database=database"   
3set conn=server.createobject("adodb.connection")   
4conn.open dsn   
1 dim file,file1,file2   
2file=trim(request.form("myface"))   
3file1=trim(request.form("myface1"))   
4file2=trim(request.form("myface2"))   
5set rs=server.createobject("adodb.recordset")   
6sql="Insert Into sFile(image,image1,image2) Values('"& file &"','"& file1&"','"& file2&"')"   
7conn.execute(sql)   
8response.redirect "ok.asp"   

呵呵,代码多了点。复制以上的代码到你的机器上运行是不是你想要的结果!至于显示图片相信大家都明白了吧!我就不必多说了。值得一提的是此代码不仅可以用于图片上传而是任何格式的文件都可以哦,至于文件大小也可以由你自己来定义哦!还有的就是我只定义了上传三个要是要上传更多的话稍为改下代码就可以了。仔细看代码就知道了是不是觉得很方便和实用。这个只不过是在我解决这个问题的时候用的一种方法而已。你要是有更好的方法来实现也可以来EMAIL告诉我!互相学习共同进步。第一次写文章可能表达方面有语法问题。要是有什么问题也可以来EMAIL告诉我!我的邮箱:[email protected],QQ:11313112。时间不早了,我要睡觉了!呼噜呼噜zzzZZZ....
(特别感谢hooke等高手一直以来的技术支持)小汪仔于2002/5/26晚

Published At
Categories with Web编程
Tagged with
comments powered by Disqus