
ASP + ACCESS 上传图片到数据库与将图片读出数据库显示之实现(详细版)
ASP上传图片至数据库内功能的实现
一般的无组件上传类,其上传过程是将图片先保存到指定文件夹,与此同时将该路径保存至数据库字段的。显示图片则是根据数据库表中的路径字段对应显示的。当然有关图片的管理,比如删除:只删除了路径,实际的图片需要根据该路径通过FSO进行删除……那有没有这样一种情况:将图片直接作为一个字段的值保存。对图片的操作就象是对数据字段的操作一样熟练。答案是肯定的,只是将该字段的类型设为OLE对象
知识点:OLE 对象字段用来存储诸如 Microsoft Word 或 Microsoft EXCEL 文档、图片、声音的数据以及在别的程序中创建的其他类型的二进制数据。OLE 对象可以链接或嵌入到 Microsoft Access 表的字段中。
一、设计数据库testimg.mdb
为方便调试,设计表imgurl,其中两个字段:id(自动编号、关键字)、img(OLE 对象)
二、连接数据库文件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>
四、接受数据并添加记录页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"
%>
五、提取数据库表中图片字段内容以做显示图片页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
%>
Disclaimer: The content on this page is collected and edited by Jinghuang Network for reference only. We do not claim ownership or bear legal responsibility for external materials. If you find any copyright infringement, please contact us with proof, and we will remove the content within 5 working days. For more insights on Global Web Optimization and Global Web Development, please visit our official site (www.0731jianzhan.com).
24/7 Support
Global hotline anytime
50x Compensation
For service downtime
7-Day Refunds
On eligible products
Pro Teams
Global branch service
10+ Yrs History
Global Branches
Senior Devs
10K+ Clients
Partner
Grow and win together
Build marketing ecosystems
Connect
Seamless user bridges
Future
Shared platform dev
0731-82272030
Address: Room 1202, North Bldg, Xingwei Mingzuo, Yuhua Dist, Changsha
Jinghuang Network specializes in corporate sites, e-commerce, apps, and full-stack systems.
We provide stable, high-performance platforms with full tech support and maintenance.
WeChat Official