实现图片的二进制保存及页面显示(ASP + Access)

数据库表:
user
字段    数据类型      说明
id     自动编号      图片id
img     OLE 对象     保存图片
 

用户上传页面(3.asp):
 
<html>
<body>
<center>
<form name=”mainForm” enctype=”multipart/form-data” action=”2.asp” method=post>
<input type=file name=mefile><br>
<input type=submit name=ok value=”OK”>
</form>
</center>
</body>
</html>
 
保存图片页面(2.asp):
 
<%
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.open”provider=microsoft.jet.oledb.4.0;data source=E:\PHP\valve.mdb;”
set rec=server.createobject(“ADODB.recordset”)
rec.Open “SELECT * FROM [user]”,connGraph,1,3
rec.addnew
rec(“img”).appendchunk mydata
rec.update
rec.close
set rec=nothing
set connGraph=nothing
%>
 
图片读取页面(1.asp):
 
此页面不允许有 html 代码,否则会出现乱码!
<%
set connGraph=server.CreateObject(“adodb.connection”)
connGraph.open”provider=microsoft.jet.oledb.4.0;data source=E:\PHP\valve.mdb;”
set rec=server.createobject(“ADODB.recordset”)
strsql=”select * from [user] where id=”&trim(request(“id”))
rec.open strsql,connGraph,1,1
Response.ContentType = “image/*”
Response.BinaryWrite rec(“img”).getChunk(8000000)
%>
 
getChunk() 中的数字必须大于等于要显示的图片字节数,否则图片显示不完全!
 
图片显示页面(4.asp):
 
<html>
<body>
<img src=”1.asp?id=要显示的图片id”>
</body>
</html>

发表评论