Link to home
Start Free TrialLog in
Avatar of Vadymus
Vadymus

asked on

DISPLAY IMAGE FROM DATABASE

Hello,
I am trying to display the image together with HTML code. The image is stored in the SQL Server database.
I cannot display it bacause my ASP knowledge is not that deep. However, one way I can display the image is using this code:

'upload.asp:
.....
      Response.Write "<a href=""file.asp?ID=" & rs("ID") & """>"       
      Response.Write rs("File Name") & "</a>"
.....

And this file.asp is displaying the whole image in Explorer. Once I add HTML, image cannot be displayed. But what I really need is displaying this same image with HTML code (ASP):

<% 'file.asp:
   ' Retrieves binary files from the database  
   Response.Buffer = True  
   ' ID of the file to retrieve
   Dim ID
      ID = Request("ID")    
   If Len(ID) < 1 Then
      ID = 7
   End If  
   ' Connection String
   Dim connStr
      connStr = ".........."
   Dim rs
      Set rs = Server.CreateObject("ADODB.Recordset")
      rs.Open "select * from IMG where ID = " & _
         ID, connStr, 2, 4
      If Not rs.EOF Then
         Response.ContentType = rs("Content Type")
         Response.BinaryWrite rs("File Data")
      End If  
      rs.Close
      Set rs = Nothing
%>

NOTE:
database is:
1.File Name - varchar
2.File Size - bigint
3.File Data - image
4.Content Type - varchar

Please help!!!
Avatar of alorentz
alorentz
Flag of United States of America image

Honestly, you may want to consider just storing the path to the file in SQL, and keep the picture in a folder on the server.

Much easier...then you can just

<img src="<%=rs("FilePath")%>">
Avatar of Vadymus
Vadymus

ASKER

I'd love to, but it is impossible... I have to use SQL Server... it is a very complicated system.
I am not sure...
but I guess you have to specify the content-type  to let the browser know that it is an image type...
ASKER CERTIFIED SOLUTION
Avatar of alorentz
alorentz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vadymus

ASKER

I am almost there.... That link helped me. However, when I use recordset to move to the next record with, obviously, new image, the same image appears. Why? Here is the code:

'upload.asp
response.Write "<a href=""show.asp?ID=" & rs("ID") & """><img src=""show.asp""width=""50"" height=""50"" border=""0""></a>"

'show.asp
<%@ LANGUAGE="VBSCRIPT" %>
   <%
   ' Clear out the existing HTTP header information
   Response.Expires = 0
   Response.Buffer = TRUE
   Response.Clear

   ' Change the HTTP header to reflect that an image is being passed.
   Response.ContentType = "image/jpeg"

   Set cn = Server.CreateObject("ADODB.Connection")
   ' The following open line assumes you have set up a System DataSource
   ' by the name of myDSN.
   cn.Open "........."
   Set rs = cn.Execute("SELECT [File Data] FROM IMG WHERE ID=" & request("ID") & "")'
   Response.BinaryWrite rs("File Data")
   Response.End
   %>

Thanks for your help.
Think it need to be:

<a href="show.asp?ID=<%=rs("ID")%>"><img src="show.asp?ID=<%=rs("ID")%>" width="50" height="50" border="0"></a>


You need to put the ID in str img src too.  I used inline ASP in above example but you should get the idea.
Avatar of Vadymus

ASKER

ok fixed... thanks