ASP圖片上傳[圖片直接存儲在數(shù)據(jù)庫里面]
一、設(shè)計(jì)數(shù)據(jù)庫testimg.mdb
為方便調(diào)試,設(shè)計(jì)表imgurl,其中兩個(gè)字段:id(自動編號、關(guān)鍵字)、img(OLE 對象)
二、連接數(shù)據(jù)庫文件conn.asp
<% db_path="testimg.mdb" set conn=server.CreateObject("ADODB.connection") connstr="driver={Microsoft Access Driver (*.mdb)};dbq="&server.MapPath(db_path) conn.open connstr %>
三、提供上傳圖片的表單頁面upload.html
<form action="upload.asp" method="post" enctype="multipart/form-data"> <input type="file" name="imgurl"> <input type="submit" name=ok value="ok"> </form>
四、接受數(shù)據(jù)并添加記錄頁upload.asp
<!--#include file="conn.asp"--> <% 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 rs=server.createobject("ADODB.recordset") rs.open "SELECT * FROM imgurl",conn,3,3 rs.addnew rs("img").AppendChunk myData rs.update rs.close set rs=nothing set conn=nothing response.redirect "index.asp" %>
五、提取數(shù)據(jù)庫表中圖片字段內(nèi)容以做顯示圖片頁showimg.asp
<!--#include file="conn.asp"--> <% set rs=server.createobject("ADODB.recordset") sql="select * from imgurl where id="&trim(request("id")) rs.open sql,conn,1,1 Response.ContentType="image/*" Response.BinaryWrite rs("img").getChunk(8000000) rs.close set rs=nothing set conn=nothing %>
六、顯示圖片的index.asp
<!--#include file="conn.asp"--> <% strsql="select * from imgurl" set rs=server.createobject("ADODB.recordset") rs.open strsql,conn,1,1 do until rs.eof whatid=rs("id") %> <img src="showimg.asp?id=<%=whatid%>">
<% rs.movenext loop %>
|