Sunday, March 16, 2008

Dynamically Load Images from a Database to an HTML Page

Copy Code
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>ADODB Stream Writer Image Test</title>

<script language="vbscript" type="text/vbscript">
Dim Source 'as String
Dim Connect 'as ADODB.Connection
Dim f   'As Scripting.File
Dim s   'As New ADODB.Stream
Dim rs  'As ADODB.Recordset
Connect = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=AdventureWorks;Data Source=.\SQLExpress"
Source = "SELECT * FROM Production.ProductPhoto"


Sub window_onload

   
Set s = CreateObject("ADODB.Stream")
   
Set rs = CreateObject("ADODB.Recordset")
   
   
rs.Open Source, Connect, adOpenForwardOnly
   
GetImage
   
rs.MoveNext

End Sub

Sub ShowButton
   
btn1.style.visibility = "visible"
End Sub

Sub nextimage()
   
   
btn1.style.visibility = "hidden"

   
If Not rs.eof Then
       
GetImage
       
rs.MoveNext
   
Else
       
MsgBox "No more images - resetting to BOF"
       
rs.MoveFirst
   
End If
   
   
window.setTimeout "ShowButton",400,"vbscript"


End Sub

   
Sub GetImage()
       
fName = rs.Fields("LargePhotoFIleName")
       
photo = rs.Fields("LargePhoto")
       
s.Open
   
s.Type = 1 'adTypeBinary
    s.Write photo
   
s.Flush
   
s.SaveToFile fName, 2 'adSaveCreateOverWrite
    s.Close
   
img1.src = fName
   
txt1.Value = fName
   
End Sub

</script>
</head>
<body>
   
<input id="btn1" type="button" value="click" onclick="nextimage"/>
    <
input id="txt1" type="input"/>
    <
br />
    <
br />
    <
img id="img1" />
</
body>
</html>
Technorati tags:

No comments:

Post a Comment