Cannot retriev blob from SQL

Cannot retriev blob from SQL

Post by Richard Lawso » Sat, 13 Apr 2002 16:34:54



I keep wondering if the blob is being stored correctly because I cannot
retriev an image. I have searched for example code on different sites.

Any suggestions?

'Save the blob

byteCount = Request.TotalBytes

RequestBin = Request.BinaryRead(byteCount)
Dim UploadRequest
Set UploadRequest = CreateObject("Scripting.Dictionary")

BuildUploadRequest RequestBin

email = UploadRequest.Item("email").Item("Value")

contentType =  UploadRequest.Item("blob").Item("ContentType")
filepathname = UploadRequest.Item("blob").Item("FileName")
filename = Right(filepathname,Len(filepathname)-InstrRev(filepathname,"\"))
picture = UploadRequest.Item("blob").Item("Value")

Response.ContentType = contentType
Response.binaryWrite picture

Set dbCnn = Server.CreateObject("ADODB.Connection")
dbCnn.Open Application("Connection1_ConnectionString"),
Application("Connection1_RuntimeUserName"),
Application("Connection_RuntimePassword")

strQuery = "SELECT * from BlobTable where ColId = -1"
Set rsNewsChunk = Server.CreateObject("ADODB.Recordset")

rsNewsChunk.Open strQuery, dbCnn, 2, 3
rsNewsChunk.AddNew
rsNewsChunk("CreateDate") = Now()
rsNewsChunk("filename") = filename
rsNewsChunk.fields.Item("Image").AppendChunk(picture)
rsNewsChunk.Update

'This works and saves a binary object.

'Retrieve the blob

Set dbCnn = Server.CreateObject("ADODB.Connection")
dbCnn.Open Application("Connection1_ConnectionString"),
Application("Connection1_RuntimeUserName"),
Application("Connection_RuntimePassword")

strQuery = "SELECT ColID, Image from BlobTable where ColID = 12"
Set rsNewsChunk = Server.CreateObject("ADODB.Recordset")

rsNewsChunk.Open strQuery, dbCnn, 1

while not rsNewsChunk.EOF

 Response.Write "<br>ID = " & rsNewsChunk("ColID") & "<br>"
 'Response.Write rsNewsChunk("Image")
 Response.ContentType = "image/JPEG"

 size = rsNewsChunk("Image").ActualSize
 blob = rsNewsChunk("Image").GetChunk(size)
 Response.BinaryWrite blob
 'Response.Write("<img src=" & blob & ">")

 'dim objStream
 'Set objStream = Server.CreateObject("ADODB.stream")

 'objStream.Type = adTypeBinary
 'objStream.Open
 'objStream.Write blob

 rsNewsChunk.MoveNext

wend

Thanks,

Rich Lawson

 
 
 

Cannot retriev blob from SQL

Post by oj » Mon, 15 Apr 2002 07:14:02


there are a few things here:

1. if you just want to show the picture, these two lines should do
    response.contenttype="image/JPEG"
    response.binarywrite rsNewsChunk("Image")

2. if you want to save it to a file then display it with <img> tag then

    dim objStream
    Set objStream = Server.CreateObject("ADODB.stream")
    with objStream
        .Type = adTypeBinary
        .Open
        .Write rsNewsChunk("Image")
        .savetofile server.mappath("../") & "\" & rsNewsChunk("Filename")
    end with
    Response.Write("<img src=" & rsNewsChunk("Filename") & ">")

--
-oj

http://www.rac4sql.com


Quote:> I keep wondering if the blob is being stored correctly because I cannot
> retriev an image. I have searched for example code on different sites.

> Any suggestions?

> 'Save the blob

> byteCount = Request.TotalBytes

> RequestBin = Request.BinaryRead(byteCount)
> Dim UploadRequest
> Set UploadRequest = CreateObject("Scripting.Dictionary")

> BuildUploadRequest RequestBin

> email = UploadRequest.Item("email").Item("Value")

> contentType =  UploadRequest.Item("blob").Item("ContentType")
> filepathname = UploadRequest.Item("blob").Item("FileName")
> filename =

Right(filepathname,Len(filepathname)-InstrRev(filepathname,"\"))

- Show quoted text -

Quote:> picture = UploadRequest.Item("blob").Item("Value")

> Response.ContentType = contentType
> Response.binaryWrite picture

> Set dbCnn = Server.CreateObject("ADODB.Connection")
> dbCnn.Open Application("Connection1_ConnectionString"),
> Application("Connection1_RuntimeUserName"),
> Application("Connection_RuntimePassword")

> strQuery = "SELECT * from BlobTable where ColId = -1"
> Set rsNewsChunk = Server.CreateObject("ADODB.Recordset")

> rsNewsChunk.Open strQuery, dbCnn, 2, 3
> rsNewsChunk.AddNew
> rsNewsChunk("CreateDate") = Now()
> rsNewsChunk("filename") = filename
> rsNewsChunk.fields.Item("Image").AppendChunk(picture)
> rsNewsChunk.Update

> 'This works and saves a binary object.

> 'Retrieve the blob

> Set dbCnn = Server.CreateObject("ADODB.Connection")
> dbCnn.Open Application("Connection1_ConnectionString"),
> Application("Connection1_RuntimeUserName"),
> Application("Connection_RuntimePassword")

> strQuery = "SELECT ColID, Image, Filename from BlobTable where ColID = 12"
> Set rsNewsChunk = Server.CreateObject("ADODB.Recordset")

> rsNewsChunk.Open strQuery, dbCnn, 1

> while not rsNewsChunk.EOF

>  Response.Write "<br>ID = " & rsNewsChunk("ColID") & "<br>"
>  'Response.Write rsNewsChunk("Image")
>  Response.ContentType = "image/JPEG"

>  size = rsNewsChunk("Image").ActualSize
>  blob = rsNewsChunk("Image").GetChunk(size)
>  Response.BinaryWrite blob
>  'Response.Write("<img src=" & blob & ">")

>  'dim objStream
>  'Set objStream = Server.CreateObject("ADODB.stream")

>  'objStream.Type = adTypeBinary
>  'objStream.Open
>  'objStream.Write blob

>  rsNewsChunk.MoveNext

> wend

> Thanks,

> Rich Lawson