Option Explicit On
Imports System.Data.SqlClient
Public Class filestoreadd
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents cmdUpload As System.Web.UI.WebControls.Button
Protected WithEvents fileUpload As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents Label3 As System.Web.UI.WebControls.Label
Protected WithEvents txtDescript As System.Web.UI.WebControls.TextBox
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Public Function GetByteArrayFromFileField( _
ByVal FileField As System.Web.UI.HtmlControls.HtmlInputFile) _
As Byte()
' Returns a byte array from the passed
' file field controls file
Dim intFileLength As Integer, bytData() As Byte
Dim objStream As System.IO.Stream
intFileLength = FileField.PostedFile.ContentLength
ReDim bytData(intFileLength)
objStream = FileField.PostedFile.InputStream
objStream.Read(bytData, 0, intFileLength)
Return bytData
End Function
Public Function FileFieldType(ByVal FileField As _
System.Web.UI.HtmlControls.HtmlInputFile) As String
' Returns the type of the posted file
If Not FileField.PostedFile Is Nothing Then _
Return FileField.PostedFile.ContentType
End Function
Public Function FileFieldLength(ByVal FileField As _
System.Web.UI.HtmlControls.HtmlInputFile) As Integer
' Returns the length of the posted file
If Not FileField.PostedFile Is Nothing Then _
Return FileField.PostedFile.ContentLength
End Function
Public Function FileFieldFilename(ByVal FileField As _
System.Web.UI.HtmlControls.HtmlInputFile) As String
' Returns the core filename of the posted file
If Not FileField.PostedFile Is Nothing Then _
Return Replace(FileField.PostedFile.FileName, _
StrReverse(Mid(StrReverse(FileField.PostedFile.FileName), _
InStr(1, StrReverse(FileField.PostedFile.FileName), "\"))), "")
End Function
Private Sub cmdUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpload.Click
Dim filelength As Integer
Dim filedata(filelength) As Byte
Dim filetype As String
Dim filename As String
filelength = FileFieldLength(fileUpload)
filedata = GetByteArrayFromFileField(fileUpload)
filetype = FileFieldType(fileUpload)
filename = FileFieldFilename(fileUpload)
Dim addQuery As String
Dim filedescription As String
Dim filedate As String
Dim uploadperson As String
filedescription = txtDescript.Text
filedate = Now.Date.ToString
If Session("Usernme") = Nothing Then
uploadperson = "taylorce"
Else
uploadperson = Session("Usernme")
End If
Dim sqlconn As SqlConnection = New SqlConnection
Dim strConn As String
strConn = "Server=SERVER;Database=test;User ID=testuser;Password=Password1"
sqlconn.ConnectionString = strConn
sqlconn.Open()
addQuery = "INSERT INTO docstore(docname, doclength, doctype, docdescription, uploadby, uploaddate, actualdoc) VALUES_
(" & filename & ", '" & filelength & "', " & filetype & ", '" & txtDescript.Text & "', _
" & uploadperson & ", " & filedate & ", @data)"
Dim aCommand As New SqlCommand(addQuery, sqlconn)
aCommand.Parameters.Add("@data", SqlDbType.Image, filedata.Length, "actualdoc")
aCommand.ExecuteNonQuery()
sqlconn.Close()
Page.RegisterStartupScript("script2", "<script language=javascript>alert('Your file has been added.');</script>")
End Sub
End Class