關于圖片上傳的例子在網上有很多文章和原代碼。但是每次上論壇瀏覽帖子的時候都是看到很多網友對圖片上傳感到頭疼和麻煩。其實這個問題也是曾經讓我感到頭疼。也看過了不少的文章和代碼?,F在我寫的這篇文章是把我比較喜歡的一篇代碼和動網里的高手對這篇代碼優化后再加上我增加一些代碼結合出來的!呵呵,其實這篇文章是沾了寫這篇代碼的人(稻香居士)和動網里那些高手的光。:)
好了,不說廢話了。開始來搭建所需要的環境和數據結構!
先新建一個名字叫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=”ja;vascript: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=””ja;vascript: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.Dictionary”)
????????set upfile_5xSoft_Stream=CreateObject(“Adodb.Stream”)
????????upfile_5xSoft_Stream.mode=3
????????upfile_5xSoft_Stream.type=1
????????upfile_5xSoft_Stream.open
????????upfile_5xSoft_Stream.write Request.BinaryRead(Request.TotalBytes)
????????
????????vbEnter=Chr(13)&Chr(10)
????????iDivLen=inString(1,vbEnter)+1
????????strDiv=subString(1,iDivLen)
????????iFormStart=iDivLen
????????iFormEnd=inString(iformStart,strDiv)-1
????????while iFormStart < iFormEnd
??????????iStart=inString(iFormStart,”name=”””)
??????????iEnd=inString(iStart+6,””””)
??????????mFormName=subString(iStart+6,iEnd-iStart-6)
??????????iFileNameStart=inString(iEnd+1,”filename=”””)
??????????if iFileNameStart>0 and iFileNameStart<iFormEnd then
???????????iFileNameEnd=inString(iFileNameStart+10,””””)
???????????mFileName=subString(iFileNameStart+10,iFileNameEnd-iFileNameStart-10)
???????????iStart=inString(iFileNameEnd+1,vbEnter&vbEnter)
???????????iEnd=inString(iStart+4,vbEnter&strDiv)
???????????if iEnd>iStart then
????????????mFileSize=iEnd-iStart-4
???????????else
????????????mFileSize=0
???????????end if
???????????set theFile=new FileInfo
???????????theFile.FileName=getFileName(mFileName)
???????????theFile.FilePath=getFilePath(mFileName)
???????????theFile.FileSize=mFileSize
???????????theFile.FileStart=iStart+4
???????????theFile.FormName=FormName
???????????file.add mFormName,theFile
??????????else
???????????iStart=inString(iEnd+1,vbEnter&vbEnter)
???????????iEnd=inString(iStart+4,vbEnter&strDiv)
????????
???????????if iEnd>iStart then
????????????mFormValue=subString(iStart+4,iEnd-iStart-4)
???????????else
????????????mFormValue=””
???????????end if
???????????form.Add mFormName,mFormValue
??????????end if
????????
??????????iFormStart=iformEnd+iDivLen
??????????iFormEnd=inString(iformStart,strDiv)-1
????????wend
End Sub
Private Function subString(theStart,theLen)
dim i,c,stemp
upfile_5xSoft_Stream.Position=theStart-1
stemp=””
for i=1 to theLen
???if upfile_5xSoft_Stream.EOS then Exit for
???c=ascB(upfile_5xSoft_Stream.Read(1))
???If c > 127 Then
????if upfile_5xSoft_Stream.EOS then Exit for
????stemp=stemp&Chr(AscW(ChrB(AscB(upfile_5xSoft_Stream.Read(1)))&ChrB(c)))
????i=i+1
???else
????stemp=stemp&Chr(c)
???End If
Next
subString=stemp
End function
Private Function inString(theStart,varStr)
dim i,j,bt,theLen,str
InString=0
Str=toByte(varStr)
theLen=LenB(Str)
for i=theStart to upfile_5xSoft_Stream.Size-theLen
???if i>upfile_5xSoft_Stream.size then exit Function
???upfile_5xSoft_Stream.Position=i-1
???if AscB(upfile_5xSoft_Stream.Read(1))=AscB(midB(Str,1)) then
????InString=i
????for j=2 to theLen
??????if upfile_5xSoft_Stream.EOS then
????????inString=0
????????Exit for
??????end if
??????if AscB(upfile_5xSoft_Stream.Read(1))<>AscB(MidB(Str,j,1)) then
????????InString=0
????????Exit For
??????end if
????next
????if InString<>0 then Exit Function
???end if
next
End Function
Private Sub Class_Terminate??
??form.RemoveAll
??file.RemoveAll
??set form=nothing
??set file=nothing
??upfile_5xSoft_Stream.close
??set upfile_5xSoft_Stream=nothing
End Sub
???
Private function GetFilePath(FullPath)
??If FullPath <> “” Then
???GetFilePath = left(FullPath,InStrRev(FullPath, “\”))
??Else
???GetFilePath = “”
??End If
End??function
Private function GetFileName(FullPath)
??If FullPath <> “” Then
???GetFileName = mid(FullPath,InStrRev(FullPath, “\”)+1)
??Else
???GetFileName = “”
??End If
End??function
Private function toByte(Str)
???dim i,iCode,c,iLow,iHigh
???toByte=””
???For i=1 To Len(Str)
???c=mid(Str,i,1)
???iCode =Asc(c)
???If iCode<0 Then iCode = iCode + 65535
???If iCode>255 Then
?????iLow = Left(Hex(Asc(c)),2)
?????iHigh =Right(Hex(Asc(c)),2)
?????toByte = toByte & chrB(“&H”&iLow) & chrB(“&H”&iHigh)
???Else
?????toByte = toByte & chrB(AscB(c))
???End If
???Next
End function
End Class
Class FileInfo
??dim FormName,FileName,FilePath,FileSize,FileStart
??Private Sub Class_Initialize
????FileName = “”
????FilePath = “”
????FileSize = 0
????FileStart= 0
????FormName = “”
??End Sub
??
Public function SaveAs(FullPath)
????dim dr,ErrorChar,i
????SaveAs=1
????if trim(fullpath)=”” or FileSize=0 or FileStart=0 or FileName=”” then exit function
????if FileStart=0 or right(fullpath,1)=”/” then exit function
????set dr=CreateObject(“Adodb.Stream”)
????dr.Mode=3
????dr.Type=1
????dr.Open
????upfile_5xSoft_Stream.position=FileStart-1
????upfile_5xSoft_Stream.copyto dr,FileSize
????dr.SaveToFile FullPath,2
????dr.Close
????set dr=nothing
????SaveAs=0
??end function
End Class
</SCRIPT>
save.asp
<% dim dsn,conn
dsn=”driver={sql server};server=127.0.0.1;uid=sa;pwd=;database=database”
set conn=server.createobject(“adodb.connection”)
conn.open dsn
%>
<% dim file,file1,file2
file=trim(request.form(“myface”))
file1=trim(request.form(“myface1”))
file2=trim(request.form(“myface2”))
set rs=server.createobject(“adodb.recordset”)
sql=”Insert Into sFile(image,image1,image2) Values(‘”& file &”‘,'”& file1&”‘,'”& file2&”‘)”
conn.execute(sql)
response.redirect “ok.asp”
%>
呵呵,代碼多了點。復制以上的代碼到你的機器上運行是不是你想要的結果!至于顯示圖片相信大家都明白了吧!我就不必多說了。值得一提的是此代碼不僅可以用于圖片上傳而是任何格式的文件都可以哦,至于文件大小也可以由你自己來定義哦!還有的就是我只定義了上傳三個要是要上傳更多的話稍為改下代碼就可以了。仔細看代碼就知道了是不是覺得很方便和實用。這個只不過是在我解決這個問題的時候用的一種方法而已。你要是有更好的方法來實現也可以來EMAIL告訴我!互相學習共同進步。第一次寫文章可能表達方面有語法問題。要是有什么問題也可以來EMAIL告訴我!我的郵箱:hushuowang@163.net,QQ:11313112。時間不早了,我要睡覺了!呼嚕呼嚕zzzZZZ….
(特別感謝hooke等高手一直以來的技術支持)小汪仔于2002/5/26晚
