请问怎么从access数据库中读取图像
我从mdb中读取ole类型字段的图像,这个图像原来是用access加进去的。
代码如下:
<!--#include file= "conn.asp "-->
<%
if request( "id ") <> " " then
rid=int(request( "id "))
else
rid=1
end if
sql= "select top 1 id from [零售户] order by id desc "
set rs=conn.execute(sql)
maxid=int(rs( "id "))
sql= "select top 1 id from [零售户] order by id "
set rs=conn.execute(sql)
minid=int(rs( "id "))
if rid <1 then
rid=1
elseif rid> maxid then
response.redirect "?id= "&maxid
elseif rid <minid then
response.redirect "?id= "&minid
else
rid=rid
end if
sql= "select * from [零售户] where id= "&rid
Set rs=Server.CreateObject( "ADODB.Recordset ")
rs.Open sql,conn,1,1
Response.ContentType= "image/* "
dataSize=rs( "照片 ").actualSize
Response.BinaryWrite rs( "照片 ").getChunk(datasize)
response.End()
rs.close
set rs=nothing
call connclose()
%>
可是读出来只是红x啊。
------解决方案--------------------在ACCESS中使用了两个关键的技术来保存图形
1。使用了bmp格式
2。78个字节的文件头
<%
response.Expires = 0
response.Buffer = True
response.Clear
response.contentType = "image/bmp "
%>
接着你要干的就是去掉那78个字节的OLE对象的文件头。
<%
Const OLEHEADERSIZE = 78
nFieldSize = rs( "photo ").ActualSize
oleHeader = rs( "photo ").GetChunk(OLEHEADERSIZE)
imageBytes = rs( "photo ").GetChunk(nFieldSize - OLEHEADERSIZE)
Response.BinaryWrite imageBytes
%>
现在举一个例子:
如果你要得到一个职工的信息,这段信息包括一个介绍和他的图象。
并且要同时显示文字和图形。
代码如下:(其中的theImg是一个代理页面)
theImg.asp
<%
response.Expires = 0
response.Buffer = True
response.Clear
response.contentType = Session( "ImageType ")
response.BinaryWrite Session( "ImageBytes ")
Session( "ImageType ") = " "
Session( "ImageBytes ") = " "
response.End
%>
Function SetImageForDisplay(field, contentType)
OLEHEADERSIZE = 78
contentType = LCase(contentType)
select case contentType
case "gif ", "jpeg ", "bmp "
contentType = "image/ " & contentType
bytes = field.value
case "ole "
contentType = "image/bmp "
nFieldSize = field.ActualSize
oleHeader = field.GetChunk(OLEHEADERSIZE)
bytes = field.GetChunk(nFieldSize - OLEHEADERSIZE)
end select
Session( "imageBytes ") = bytes
Session( "imageType ") = contentType
End Function
'注意的是,程序中只使用了4中格式:gif, jpeg, bmp , ole .
<%
sql = "select * from Employees "
Set oRS = Server.CreateObject( "ADODB.Recordset ")
oRS.CursorLocation = 3