Tutorial 3: ADO Support and Saving a File as a BLOB

Dundas

Tutorial 3: ADO Support and Saving a File as a BLOB

The objectives of this tutorial are to:

  • Demonstrate how to work with an ADO recordset with a DSNless connection.

  • Show how to save an uploaded file as a BLOB.

We are assuming that a form with an EncType of "Multipart/Form-Data" is POSTING data to the ASP page which contains the following code.

Assumptions

  1. A SQL Server 7.0 database named "Temp" exists with a SQL username and password of "sa" and "", respectively.

  2. This database has a table called "Test".

  3. The table has an "image" column named "Picture".

  4. The name of the server is "SOMESERVER".

  5. The POST form uses one or more file input boxes.

Example

<% 'use inline error trapping so that any exceptions raised by the Upload control are caught
On Error Resume Next

'create an instance of the Uplaod control
Set
objUpload = Server.CreateObject("Dundas.Upload.2")

'we will temporarily save uploaded files to disk
objUpload.Save "c:\temp"

'set the connection string
strConnect = "Driver={SQL Server};Server=SOMESERVER;Database=Temp;UID=sa;PWD="

'create an ADO recordset
Set
rs = Server.CreateObject("ADODB.Recordset")

'open a recordset using the connection string
rs.Open "Test", strConnect, 2, 3

'loop through all uploaded files and save to the database as BLOBs if
' an image was actually uploaded (we use the ContentType property of

' the UploadedFile object to accomplish this)

For Each
objFile In objUpload.Files

If InStr(1,objFile.ContentType,"image") <> 0 Then

'add a new record and insert the image file
rs.AddNew
rs("Picture").Value = objFile.Binary

End If

Next

'save changes and close recordset
rs.Update
rs.Close

'release resources
Set
rs = Nothing
Set
objUpload = Nothing
%>