Quick Example:
1. Database contains just one table with following structure
FileId Autonumber Primary Key
FileName Text 255
FileContent OLE Object
2. UploadFile.aspx page HTML content
-- BEGIN CODE --
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="UploadFile.aspx.vb"
Inherits="UploadFile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="fupFile" runat="server" /><br />
<br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" /> </div>
</form>
</body>
</html>
-- END CODE --
3. UploadFile.aspx.vb code behing file
-- BEGIN CODE --
Imports System.Data
Imports System.Data.OleDb
Partial Class UploadFile
Inherits System.Web.UI.Page
Private Const ConnectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\TEMP\Files.mdb;"
Private Sub InsertFile(ByVal strFileName As String, ByVal abyContent As
Byte())
Dim strQuery As String = "INSERT INTO Files (FileName, FileContent)
VALUES (?, ?)"
Dim oConnection As New OleDbConnection(ConnectionString)
Dim oCommand As New OleDbCommand(strQuery, oConnection)
Dim oParameter As OleDbParameter = Nothing
oParameter = New OleDbParameter("?", OleDbType.VarChar)
oParameter.Value = strFileName
oParameter.Direction = ParameterDirection.Input
oCommand.Parameters.Add(oParameter)
oParameter = New OleDbParameter("?", OleDbType.VarBinary)
oParameter.Value = abyContent
oParameter.Direction = ParameterDirection.Input
oCommand.Parameters.Add(oParameter)
oCommand.CommandTimeout = 120
oCommand.CommandType = CommandType.Text
Try
oConnection.Open()
oCommand.ExecuteNonQuery()
Catch ex As Exception
Throw ex
Finally
If oConnection.State <> ConnectionState.Closed Then
oConnection.Close()
End If
End Try
End Sub
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnUpload.Click
If Not fupFile.FileContent Is Nothing Then
' perform validation logic before updating the database - i.e.
if file size is
' correct, etc.
With fupFile.FileContent
' allocate buffer for file data
Dim abyContent(CType(.Length, Integer)) As Byte
' copy data to buffer
.Read(abyContent, 0, .Length)
' insert file data to the database
Me.InsertFile(fupFile.FileName, abyContent)
End With
End If
End Sub
End Class
-- END CODE --
Hope this helps!