怎样将数据库中的图片数据读出来?

xunge 2001-11-02 04:46:52
比如是access数据库,来点代码最好!
...全文
70 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
tonnycncn 2001-11-03
  • 打赏
  • 举报
回复
一般在html中,显示图片都是使用<img>标签,也就是<img src="图片路径">,但是我们的图片是保存到了数据库中,“图片路径”是什么呢?呵呵,其实这个src属性除了指定路径外,也可以这样使用哦:<img src="showimg.asp?id=xxx">所以,我们所要做的就是在showimg.asp中从数据库中读出来符合条件的数据,并返回到src属性中就可以了,具体代码如下(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.binarywrite rec("img").getchunk(7500000)
rec.close
set rec=nothing
set conngraph=nothing
%>
注意在输出到浏览器之前一定要指定response.contenttype = "image/*",以便正常显示图片。
microyzy 2001-11-02
  • 打赏
  • 举报
回复
流程是:
读图象文件-->用appendchunk方法写到数据库中-->用getchunk方法读出来-->存成图象文件
具体的操作你可以参考ado文档。如果没有我下面给你发一个(很长)。

AppendChunk 和 GetChunk 方法范例
该范例使用 AppendChunk 和 GetChunk 方法用其他记录中的数据填写图像字段。

Public Sub AppendChunkX()

Dim cnn1 As ADODB.Connection
Dim rstPubInfo As ADODB.Recordset
Dim strCnn As String
Dim strPubID As String
Dim strPRInfo As String
Dim lngOffset As Long
Dim lngLogoSize As Long
Dim varLogo As Variant
Dim varChunk As Variant

Const conChunkSize = 100

' 打开连接。
Set cnn1 = New ADODB.Connection
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
cnn1.Open strCnn

' 打开 pub_info 表。
Set rstPubInfo = New ADODB.Recordset
rstPubInfo.CursorType = adOpenKeyset
rstPubInfo.LockType = adLockOptimistic
rstPubInfo.Open "pub_info", cnn1, , , adCmdTable

' 提示复制徽标。
strMsg = "Available logos are : " & vbCr & vbCr
Do While Not rstPubInfo.EOF
strMsg = strMsg & rstPubInfo!pub_id & vbCr & _
Left(rstPubInfo!pr_info, InStr(rstPubInfo!pr_info, ",") - 1) & _
vbCr & vbCr
rstPubInfo.MoveNext
Loop
strMsg = strMsg & "Enter the ID of a logo to copy:"
strPubID = InputBox(strMsg)

' 将徽标大块复制到变量中。
rstPubInfo.Filter = "pub_id = '" & strPubID & "'"
lngLogoSize = rstPubInfo!logo.ActualSize
Do While lngOffset < lngLogoSize
varChunk = rstPubInfo!logo.GetChunk(conChunkSize)
varLogo = varLogo & varChunk
lngOffset = lngOffset + conChunkSize
Loop

' 从用户处得到数据。
strPubID = Trim(InputBox("Enter a new pub ID:"))
strPRInfo = Trim(InputBox("Enter descriptive text:"))

' 添加新记录,大块复制徽标。
rstPubInfo.AddNew
rstPubInfo!pub_id = strPubID
rstPubInfo!pr_info = strPRInfo

lngOffset = 0 ' 重置位移。
Do While lngOffset < lngLogoSize
varChunk = LeftB(RightB(varLogo, lngLogoSize - lngOffset), _
conChunkSize)
rstPubInfo!logo.AppendChunk varChunk
lngOffset = lngOffset + conChunkSize
Loop
rstPubInfo.Update

' 显示新添加的数据。
MsgBox "New record: " & rstPubInfo!pub_id & vbCr & _
"Description: " & rstPubInfo!pr_info & vbCr & _
"Logo size: " & rstPubInfo!logo.ActualSize

' 删除新记录,因为这只是演示。
rstPubInfo.Requery
cnn1.Execute "DELETE FROM pub_info " & _
"WHERE pub_id = '" & strPubID & "'"

rstPubInfo.Close
cnn1.Close

End Sub


28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧