There has been a endless discussion on the internet about the problems of storing images in databases and retrieving them with ASP. Some people say it is possible, but don't have the code, while others say it is only possible using MS SQL and not using MS Access.
I have been looking for the solutions and found it: it is very well possible, even when using Access, and I have the code for you:
I assume that you have created in Access a database db1.mdb with a table Table1 and a field Field1 of the OLE type. This is the Visual Basic code for storing an image:
Dim a() As Byte, b As String, c, i, l, r
Open "image.gif" For Binary As 1
l = LOF(1)
b = Space(l)
Get #1, , b
Close 1
ReDim a(l)
For i = 0 To l - 1
a(i) = CByte(Asc(Mid(b, i + 1, 1)))
Next i
Set c = CreateObject("ADODB.Connection")
c.Open "driver={Microsoft Access Driver (*.mdb)};dbq=db1.mdb"
Set r = CreateObject("ADODB.RecordSet")
r.Open "SELECT * FROM Table1", c, 2, 2
If r.EOF Then
r.addnew
End If
r(0) = a
r.Update
r.Close
This is the ASP code for retrieving the image:
<%
Response.Buffer = true
Response.Clear
Response.Expires = 0
Response.ContentType = "image/gif"
Set c = Server.CreateObject("ADODB.Connection")
c.Open "driver={Microsoft Access Driver *.mdb)};dbq=inetpubwwwrootdb1.mdb"
Set r = CreateObject("ADODB.RecordSet")
r.Open "SELECT * FROM Table1", c, 2, 2
Response.BinaryWrite r(0)
r.close
Response.End
%>
The only assumption I have now is that the length of the image is not longer than the maximum variable size. Otherwise use the chunk-method.
|