请那位帮忙给去详细的代码
谢谢了!
如何把图片上传到数据库中并显示出来?
好了,看看我们的代码吧: upload.htm
' 上传页面
<html>
<body>
<p align="center">精彩春风之图片上传</p> <center> <form name="mainForm" enctype="multipart/form-data"
' 这个Form属性是得到上传的数据的关键
action="process.asp" method=post>
<input type=file name=mefile><br>
<input type=submit name=ok value="上传">
</form>
</center>
</body>
</html>
process.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.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
%>
数据库结构如下:
Access:
序号 | 字段名称 | 类 型 | 描 述 |
1 | id | 自动编号 | 主键值 |
2 | img | OLE对象 | 用来保存图片数据 |
MS SQL Server:
序号 | 字段名称 | 类 型 | 描 述 |
1 | id | int(Identity) | 主键值 |
2 | img | image | 用来保存图片数据 |
写入数据库代码 <% dim rs dim formsize,formdata,bncrlf,divider,datastart,dataend,mydata 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) %> <!--#include file="conn.asp"--> <% Sqlcmd="SELECT * from pic where idname='"&session("idtu")&"'" set res=conn.Execute(Sqlcmd) if res.EOF Then
sql="select * from pic order by id desc"
set rs=conn.execute(sql) id=rs("id") Set rs = Server.CreateObject("ADODB.Recordset") rs.Open "pic",conn,3,2 rs.addnew rs("big").appendchunk mydata rs("idname")=session("idtu") rs.update set rs=nothing set conn=nothing response.redirect "sendphoto.asp" %> <% else %> <script language="javascript"> alert("本站限制一张图片,若想更新,请删除原图片!") window.location="../dianjiadenglu1.asp" </script> <% end if %>