Image from SQL Server to ASP Page
Dear Friends
How can i read a image stored in a database (SQL SERVER ) and
display it in an Asp page.
Please help me in this
Bye
Pramod Kumar R
# 2 Re: Image from SQL Server to ASP Page
<%@ Language=VBScript %>
<%
Response.Expires = -1
Response.ExpiresAbsolute
Response.contentType = "image/gif"
Dim CnToDb
Set CnToDb=Server.CreateObject("ADODB.Connection")
Connstring = "DRIVER={SQL Server};SERVER=NEWSERVER;UID=sa;PWD=sqlpass;DATABASE=XManufacturing"
CnToDb.Open (Connstring)
txtcustid = Request.Form("txtcustid")
Dim fld, byteChunk, strNote, Offset, Totalsize, Remainder, NumOfChuncks,
CurrentRecPos
Dim FieldSize, FileNumber, HeaderSize, ChunkSize, txtRecCnt, TheFileAcessor
set rs = Server.CreateObject("ADODB.Recordset")
if trim(txtcustid)<>"" then
rs.Open "select a.customer_code Code ,a.customer_name Name,b.photo photo
from customer_mast a inner join crmcontact b on a.customer_code=b.customer_code
where b.Customer_Code='"& txtcustid & "'",cntodb,3
end if
TempFileBase = "tempfile.tmp"
pathphysical = Server.MapPath("./photos")
TempFileDrive = pathphysical & "\"
TempFileHttp = "./photos/"
if trim(txtcustid)<>"" then
if not rs.EOF then
Set TheFileAcessor = CreateObject("FileAccessor.FileWriter")
HeaderSize = 78
ChunkSize = 100
Set Flds = rs.Fields
For Each fld In Flds
FieldSize = fld.ActualSize
Select Case fld.Name
Case "Code"
fldid = trim(fld.Value)
Case "Name"
fldName = trim(fld.Value)
Case "photo"
on error resume next
TempFile = TempFileDrive & TempFileBase & "*"
TheFileAcessor.RemoveFile TempFile
TempFile = TempFileDrive & TempFileBase & rs(0) & ".gif"
TheFileAcessor.OpenFile TempFile
Totalsize = FieldSize - HeaderSize ' Substract
it from the total size.
byteChunk = fld.GetChunk(HeaderSize) ' Get rid of the
header.
fldPhotocheck = false
if isnull(byteChunk) or isempty(byteChunk) then
fldPhotocheck = true
end if
TheFileAcessor.WriteToFile byteChunk
NumOfChuncks = Totalsize \ ChunkSize
Remainder = Totalsize Mod ChunkSize
If Remainder > 0 Then
byteChunk = fld.GetChunk(Remainder)
TheFileAcessor.WriteToFile byteChunk
End If
Offset = Remainder
Do While Offset < Totalsize
byteChunk = fld.GetChunk(ChunkSize)
TheFileAcessor.WriteToFile byteChunk
Offset = Offset + ChunkSize
Loop
TheFileAcessor.CloseFile
fldPhoto = "<img src='" + TempFileHttp + TempFileBase + rs(0) + ".gif'
width='100' height='120'></img>"
if fldPhotocheck then
fldPhoto = "<img src='" + TempFileHttp + "no.gif' width='100' height='120'></img>"
end if
End Select
Next
else
txtmessage = "Customer code " & txtcustid & " could not be found<br>"
end if
end if
%>
<HTML>
<HEAD>
</HEAD>
<BODY>
<%Response.Write "<font face='Verdana,Arial' size='1' color='red'>" & txtmessage
& "</font>"%>
<form name="frm" action="customer.asp" method="post">
<table width="100%">
<tr>
<td colspan="2"><font face="Verdana, Arial, Helvetica, sans-serif"
size="1"><b>Customer
Details</b></font></td>
</tr>
<tr>
<td width="59"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
ID</font></td>
<td width="708"> <font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<input name="txtcustid" value="<%=fldid%>" size="10">
<input type="submit" value="Go">
</font></td>
</tr>
<tr>
<td width="59"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
Name</font></td>
<td width="708"> <font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<input name="txtcustname" value="<%=fldname%>" size="20">
</font></td>
</tr>
<tr>
<td width="59"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
Photo </font></td>
<td width="708"> <font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<%if trim(fldPhoto)="" then
Response.Write "<img src='" + TempFileHttp + "no.gif' width='100' height='120'></img>"
else
Response.Write fldPhoto
end if%>
</font></td>
</tr>
</table>
</form>
</BODY>
</HTML>
koshy at 2007-11-11 23:18:59 >
