Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SQL and ASP to display Images

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-01-24 : 09:18:23
Sam writes "Hi,

I have read other articles with relation to displaying images using SQL and ASP. Here is my scenario.

I have 1 table with 3 columns ImageLoc <varchar> Product <char> and Availability <char>

When a user makes a selection from the ASP site, i would like the record to be displayed (and the image based on the pathname in the ImageLoc field)

The database side is done.. Can someone give me some sample ASP code to work with that will display the Image and it's associated record.

Here is my code

@LANGUAGE="VBSCRIPT" CODEPAGE="1252"
<!--#include file="file:///C|/Documents%20and%20Settings/Admin/My%20Documents/test.asp" -->

Dim bobjRS
Dim bobjRS_numRows

Set bobjRS = Server.CreateObject("ADODB.Recordset")
bobjRS.ActiveConnection = MM_test_STRING
bobjRS.Source = "SELECT * FROM dbo.Inventory"
bobjRS.CursorType = 0
bobjRS.CursorLocation = 2
bobjRS.LockType = 1
bobjRS.Open()

bobjRS_numRows = 0

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<A>
=(bobjRS.Fields.Item("ImageLoc").Value) =(bobjRS.Fields.Item("ProductMake").Value) =(bobjRS.Fields.Item("ProductType").Value) =(bobjRS.Fields.Item("CostPrice").Value)
</body>
</html>

bobjRS.Close()
Set bobjRS = Nothing"

ValterBorges
Master Smack Fu Yak Hacker

1429 Posts

Posted - 2003-01-24 : 09:30:17
Create an asp page callit images.asp

Response.Buffer = True
strFileName=Request("Image")
strFilePath=server.mappath("/images/"&strFilename)
Const adTypeBinary = 1
Response.Clear
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile strFilePath
strFileType = "image/jpeg" ' change to the correct content type for your file
Response.AddHeader "Content-Disposition", "attachment; filename=" & strFileName
Response.AddHeader "Content-Length", strFileSize
Response.Charset = "UTF-8"
Response.ContentType = strFileType
Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing


Then in you asp that you are creating from your recordset include an image tag with a src as such.

<img src="images.asp?Image=Test.jpg">

This should do it.



Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2003-01-25 : 00:56:34
I think this is a simpler approach for your problem than the one Valter proposes:

First, I highly recommend writing a stored procedure and not executing a string as you have, but that's another story.

Looks like you want to write all images in a recordset selected by "*"? You'll need to add a loop.

Leaving your ADO as-is, here's what I'd do with the HTML in ASP.

WITH bobjRS
response.write "<IMG SRC=""" & .Fields.Item("ImageLoc").Value & ">"
response.write "<br>Make: " & .Fields.Item("ProductMake").Value
response.write "<br>Type" " & .Fields.Item("ProductType").Value
response.write "<br>Item: " & .Fields.Item("CostPrice").Value
END WITH

There's really nothing to writing an HTML <IMG> from ASP. I've used Valter's approach when I generated a graphic using a library, but for loading an existing filename it seems complex.

SamC

Go to Top of Page
   

- Advertisement -