如何把圖片上傳到數(shù)據(jù)庫中并顯示出來?
upload.htm
' 上傳頁面.
html>
body>
p align="center">千花飛舞之圖片上傳/p>
center>
form name="mainForm" enctype="multipart/form-data"
' 這個Form屬性是得到上傳的數(shù)據(jù)的關鍵.
action="process.asp" method=post>
input type=file name=mefile>br>
input type=submit name=ok value="上傳">
/form>
/center>
/body>/html>
process.asp
' 處理瀏覽器中送來的數(shù)據(jù).
%
response.buffer=true
formsize=request.totalbytes
formdata=request.binaryread(formsize)
bncrlf=chrB(13) chrB(10)
divider=leftB(formdata,clng(instrb(formdata,bncrlf))-1)
datastart=instrb(formdata,bncrlf bncrlf)+4
dataend=instrb(datastart+1,formdata,divider)-datastart
mydata=midb(formdata,datastart,dataend)
set connGraph=server.CreateObject("ADODB.connection")
connGraph.ConnectionString="driver={Microsoft Access Driver (*.mdb)};
DBQ=" server.MapPath("images.mdb") ";uid=;PWD=;"
connGraph.Open
set rec=server.createobject("ADODB.recordset")
rec.Open "select * from [images] where id is null",connGraph,1,3
rec.addnew
rec("img").appendchunk mydata
rec.update
rec.close
set rec=nothing
set connGraph=nothing
%>
showimg.asp
' 顯示圖片.
%
set connGraph=server.CreateObject("ADODB.connection")
connGraph.ConnectionString="driver={Microsoft Access Driver (*.mdb)};
DBQ=" server.MapPath("images.mdb") ";uid=;PWD=;"
connGraph.Open
set rec=server.createobject("ADODB.recordset")
strsql="select img from images where id=" trim(request("id"))
rec.open strsql,connGraph,1,1
Response.ContentType = "image/*"
' 在輸出到瀏覽器之前一定要指定Response.ContentType = "image/*",以便正常顯示圖片.
Response.BinaryWrite rec("img").getChunk(7500000)
rec.close
set rec=nothing
set connGraph=nothing
%> [1]