Recover Varbinary Asp

Asked

Viewed 27 times

0

Good afternoon, I wonder how I can recover an image saved in a database like varbinary for an img tag on Asp. My code is as follows:

<%
   dim objCmdI, RsImage, vsImage
   call AbreConexao()

   Set RsImage = Server.CreateObject("ADODB.RecordSet")
   RsImage.Open "select Imagem from Usuario", 
   Conn

   vsImage = RsImage("Imagem")      
   Call FechaConexao()
%>

This is the tag I have

<img src="<%=response.Write(vsImage)%>" width="87" height="104" border="1">

The code of the Abreconexao method() :

Sub AbreConexao
Call validaAcesso(trim(request.ServerVariables("PATH_INFO")))
Set Conn = Server.CreateObject( "ADODB.Connection" )
Conn.Mode = 12
Conn.Open strConn
end sub

Note: the connection call and tag are inside the same Div. Obs2: the code Abreconexao() is in another document, but it works perfectly for other calls in the code, I’m sure the problem is not connection to the database, but rather how I am making the request to the database.

I have never worked with pure Asp code and I took it to do. I thank anyone who can help.

1 answer

0

Well, I was able to recover the bank image as follows. I created a new file "Mostrar imagem.Asp", this page should contain only a script that will fetch the image in the database and show on the screen, the code of this page should be something close to it:

<%@ LANGUAGE="VBSCRIPT" %>
<%
 'Variável criada para receber um parâmetro para seleção da imagem
 Dim strID

 strID = request("userID")

 Response.Expires = 0
 Response.Buffer = TRUE
 Response.Clear
 'este campo deve ser ajustado de acordo com o formato da imagem que deseja buscar
 Response.ContentType = "image/jpg"

 Set cn = Server.CreateObject("ADODB.Connection")

 cn.Open "Driver= 

 {SQLServer};Description=sqldemo;SERVER=IP_Do_Servidor;UID=Usuario_Banco;
  PWD=Senha_Banco;DATABASE=Nome_Banco"
 'Query que busca a imagem no banco
 Set rs = cn.Execute("select Imagem from Tabela_Imagem where Compo = " & 
 strID)
 Response.BinaryWrite rs("Cd_Foto")
 Response.End
 %>

To call the script open the file that contains your tag and add the following code:

<img src="MostrarImagem.asp?userID=Valor" border="1" height="200"  width="200" />

The "userid" parameter is optional, it serves to pass a specific value to the script to fetch the image in the database. If you want to call direct just use:

<img src="MostrarImagem.asp" border="1" height="200"  width="200" />

With the code described above I was able to solve my problem, remembering that the field that stores the image in the bank should be varbinary (MAX).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.