Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image from binary data

Status
Not open for further replies.

aarellano

MIS
Oct 22, 2007
168
US
Hello,

I have a table on a db2 database that has a binary field. I was told that this field is an image field. I am trying to write an asp page that will display the field.
Here is what I have done

I created an ODBC connection to my DB2 database.

created an sql statement and can see the different fields

Here is the code that I am using.

Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="../cls/Connections/agpl.asp" -->
<%
Dim Recordset1
Dim Recordset1_numRows

Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_agpl_STRING
Recordset1.Source = "select sfid, sfsig from agpl.salefil where sfid = '119141226'"
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()

Recordset1_numRows = 0
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<%

'Set the ContentType to image/gif
Response.ContentType = "image/png"

'Send the binary bits to the browser
Response.BinaryWrite(objRS("sfsig"))

%>
</body>
</html>
<%
Recordset1.Close()
Set Recordset1 = Nothing
%>

any help is much appreciated. I am really stummped on this one.
 
try chunking the data and make sure the image is the format you are telling the browser it is

add in

Code:
      mFieldSize          = LenB(ors("Evidence"))
      mBytes              = ors("Evidence").GetChunk(mfieldsize)
  
    Response.BinaryWrite mBytes

Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 
Bastien,

I tried this
Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="../cls/Connections/agpl.asp" -->
<%
Dim Recordset1
Dim Recordset1_numRows

Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_agpl_STRING
Recordset1.Source = "select sfid, sfsig from agpl.salefil where sfid = '119141226'"
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()

Recordset1_numRows = 0
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<%



      mFieldSize          = LenB(ors("sfsig"))
      mBytes              = ors("sfsig").GetChunk(mfieldsize)
  
    Response.BinaryWrite mBytes


%>
</body>
</html>
<%
Recordset1.Close()
Set Recordset1 = Nothing
%>

And got the following:


Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'ors'
/img/sfig.asp, line 30
 
1. You didn't change the db connection from ors to your Recordset1

2. you can't output html on the page as it then sends the wrong header for the content type to the browser..the normal way to approach this is to add this page as the source attribute in the main file

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<img src='getImage.asp?id=119141226' />
</body>
</html>

and the getImage.asp
Code:
%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="../cls/Connections/agpl.asp" -->
<%
Dim Recordset1
Dim Recordset1_numRows

Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_agpl_STRING
Recordset1.Source = "select sfid, sfsig from agpl.salefil where sfid = '119141226'"
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()

Recordset1_numRows = 0

'Set the ContentType to image/gif
Response.ContentType = "image/png"

mFieldSize  = LenB(Recordset1("sfsig"))
mBytes      = Recordset1("sfsig").GetChunk(mfieldsize)
  
Response.BinaryWrite mBytes
Recordset1.Close
Set Recordset1 = Nothing
%>

Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 

Bastien,

Thank you for the corrections. I really appreciate you giving me a hand. I am not getting any errors on the page but I still cannot see the image I get a square with a red X

I am trying to find out what type of image it is being inputed but have not had any luck yet.

Is there a way to find out what kidn of image it is?
 
You could check the extension on the filename and build a select statement to write the correct content type

Code:
sExt =  right(sFileName,3)

select case (lcase(sExt))
  case "jpg"
    'Set the ContentType to image/jpg
    Response.ContentType = "image/jpg"
  case "png"
    pn'Set the ContentType to image/png
    Response.ContentType = "image/png"
  case "pdf"
    'Set the ContentType to pdf
        Response.ContentType = "application/pdf"

   case else 'download the file
 ' add the header with the download file's name
	.AddHeader "Content-Disposition", "filename=" & strFileToDownload

	 ' change the content type from text/html to
	 ' the downloadable one...
	.ContentType = "application/octet-stream"

end select

Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 
yes, before you send the response content type down

Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top