PostedFile Error
Hello, I am having a very difficult time trying to upload files to a database. I have read tutorials on this and I have copied and pasted codes from one of them in my aspx page. The code is as follows:
Public Class Upload
Inherits System.Web.UI.Page
Protected WithEvents File As System.Web.UI.HtmlControls.HtmlInputFile
If FileUpload.PostedFile Is Nothing OrElse String.IsNullOrEmpty(FileUpload.PostedFile.FileNam e) OrElse FileUpload.PostedFile.InputStream Is Nothing Then
... Show error message ...
Exit Sub
End If
'Make sure we are dealing with a JPG or GIF file
Dim extension As String = Path.GetExtension(FileUpload.PostedFile.FileName). ToLower()
Dim MIMEType As String = Nothing
Select Case extension
Case ".gif"
MIMEType = "image/gif"
Case ".jpg", ".jpeg", ".jpe"
MIMEType = "image/jpeg"
Case ".png"
MIMEType = "image/png"
Case Else
'Invalid file type uploaded
... Show error message ...
Exit Sub
End Select
'Connect to the database and insert a new record into Products
Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrin gs("ImageGalleryConnectionString").ConnectionStrin g)
Const SQL As String = "INSERT INTO [Pictures] ([Title], [MIMEType], [ImageData]) VALUES (@Title, @MIMEType, @ImageData)"
Dim myCommand As New SqlCommand(SQL, myConnection)
myCommand.Parameters.AddWithValue("@Title", PictureTitle.Text.Trim())
myCommand.Parameters.AddWithValue("@MIMEType", MIMEType)
'Load FileUpload's InputStream into Byte array
Dim imageBytes(FileUpload.PostedFile.InputStream.Lengt h) As Byte
FileUpload.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length)
myCommand.Parameters.AddWithValue("@ImageData", imageBytes)
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()
End Using
End Sub
when I tried to build the solution it says that "PostedFile is not a member of System.Web.UI.WebControl.Button". Also "...Show error ..." caused an error which says "Identifier expected". It also said "Path" is not declared. I don't know what namespace to include in order to make this application work. Thank you in advance for you help.
|