This example shows how to upload an image and insert it into a database.



Our multipart HTML form contains one file field for upload.

<form method="post" action="dbupload.asp" enctype="multipart/form-data">
<input type="file" name="filefield"><br>
<input type="submit" name="submit" value="submit">


We installed SQLServer 7 on our CC2 server. We created a database called "mydbase" and inserted a table called "mytable" with an Image column called "myimage". Finally we created a user "myuser" with password "password" and read/write access to the database.

In the code below we first check to see that an image file has been uploaded. If it has we connect to the database and then open a RecordSet. We add a new record and put the field data from the upload into the "myimage" column. Finally we update the RecordSet and close our connection.

<% @Language="VBScript" %>
Set theForm = Server.CreateObject("ABCUpload4.XForm")
Set theField = theForm("filefield")(1)

If theField.FileExists and theField.ImageType <> 0 Then
  Set cn = Server.CreateObject("ADODB.Connection")
  theConn = "Provider=SQLOLEDB;Data Source=CC2;Initial Catalog=mydbase;User Id=myuser;Password=password"
  cn.Open theConn
  Set rs = Server.CreateObject("ADODB.Recordset")
  rs.Open "mytable", cn, 1, 3
  rs("myimage").Value = theField.Data
End If

File uploaded...