Hello

I am trying to build a file upload page where a user can upload files and different messages appear on screen to the user of, for example, the name and size of the file successfully uploaded, whether or not he has even selected a file to be uploaded, and a couple of error messages such as 'you have selected the wrong type of file' or 'the file is too big'.

In my aspx file, I have:

 <asp:FileUpload ID="FileUpload1" runat="server" /><br />

 <asp:Button ID="btnUpload" runat="server" Text="Upload" />

 <asp:label runat="server" id="messageArea" CssClass="iForget" style='display: none'></asp:label>    

</asp:Content>

In my aspx.vb file, I have:

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Configuration
Imports Microsoft.AspNet.Identity
Imports Microsoft.AspNet.Identity.EntityFramework
Imports Microsoft.AspNet.Identity.Owin
Imports Microsoft.Owin.Security
Imports System.Web.Security
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.OleDb

Partial Class UploadTest
    Inherits System.Web.UI.Page

    Dim Label As Object    

    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        If FileUpload1.HasFile Then
            Try
                FileUpload1.SaveAs("C:\Uploads\" & _
                   FileUpload1.FileName)
                messageArea.Text = "File name: " & _
                     FileUpload1.PostedFile.FileName & "<br>" & _
                     "File Size: " & _
                     FileUpload1.PostedFile.ContentLength & "<br>" & _
                     "Content type: " & _
                     FileUpload1.PostedFile.ContentType & "<br>" & _
                     "Location Saved: C:\Uploads\" & _
                    FileUpload1.FileName
            Catch ex As Exception
                messageArea.Text = "ERROR: " & ex.Message.ToString()
            End Try
        Else : messageArea.Text = "You have not specified a file."
        End If

        messageArea.Attributes.Add("style", "display:block")

    End Sub


    Protected MaxSizeAllowed As Integer = 524288

    Protected AllowedExtensions As String() = {".doc", ".docx", ".txt", ".png", ".gif", ".bmp", ".jpg", ".jpeg"}

    'Private Property Label As Object
    'Private messageArea As Object

    Protected Sub uploadButton_Click(sender As [Object], e As EventArgs)

        If Page.IsPostBack Then

            With messageArea
                .Style.Remove(HtmlTextWriterStyle.Display)
                .Style.Add(HtmlTextWriterStyle.Display, "inline-block")
            End With

        End If

        Me.messageArea.Text = String.Empty

        Try
            If Me.IsFileValid(Me.fileUploadControl) Then
                Me.fileUploadControl.PostedFile.SaveAs("C:\Uploads\uploadedfile.txt")
                Me.messageArea.Text = "Successful Upload!<br>"
            End If
        Catch ex As Exception
            Me.messageArea.Text = "Error uploading file: " + fileUploadControl.Value & "<br>" & ex.ToString() & "<br>"
        End Try
    End Sub

    Protected Function IsFileValid(uploadControl As HtmlInputFile) As Boolean
        Dim isFileOk As Boolean = True

        If uploadControl.PostedFile IsNot Nothing Then
            Dim isSizeError As Boolean = False
            Dim isExtensionError As Boolean = False

            ' Validate for size less than MaxSizeAllowed...
            If uploadControl.PostedFile.ContentLength > MaxSizeAllowed Then
                isFileOk = False
                isSizeError = True
            Else
                isFileOk = True
                isSizeError = False
            End If

            ' If OK so far, validate the file extension...
            If isFileOk Then
                isFileOk = False
                isExtensionError = True

                ' Get the file's extension...
                Dim fileExtension As String = System.IO.Path.GetExtension(uploadControl.PostedFile.FileName).ToLower()

                For i As Integer = 0 To Me.AllowedExtensions.Length - 1
                    If fileExtension.Trim() = Me.AllowedExtensions(i).Trim() Then
                        isFileOk = True
                        isExtensionError = False

                        Exit For
                    End If
                Next
            End If

            If isSizeError Then
                Dim errorMessage As String = "The size of the file " & Convert.ToString(uploadControl.PostedFile.FileName) & " is bigger than the allowed " & MaxSizeAllowed & " (" & Convert.ToString(uploadControl.PostedFile.ContentLength) & ").<br>"
                Me.messageArea.Text = errorMessage
            End If

            If isExtensionError Then
                Dim errorMessage As String = "Your file " & Convert.ToString(uploadControl.PostedFile.FileName) & " cannot be uploaded.<br>File types allowed: .doc, .docx, .txt, .png, .gif, .bmp, .jpg, or .jpeg.<br>"
                Me.messageArea.Text = errorMessage
            End If
        End If

        Return isFileOk

        'messageArea.Attributes.Add("style", "display:block")

    End Function

End Class

I get a couple of errors: 'fileUploadControl' is not a member of 'UploadTest', and 'fileUploadControl' is not declared. It may be inaccessible due to its protection level.

The errors look like this (screenshot) Click Here

How do I make 'fileUploadControl' a member of 'UploadTest' and resolve the 'fileUploadControl' problem?

Thanks for any help.

When you're typing does intellisense provide the name of the file upload control? If it doesn'tthen something is wrong.
Actually, looking at your code again I see you gave the file upload control an ID of FileUpload1. You should be using that.

Hello Hericles

Thank you for pointing that out. One of the errors has gone, but I am still left with:

 If Me.IsFileValid(Me.**FileUpload1**) Then

Error: 'System.Web.UI.WebControls.FileUpload' cannot be converted to 'System.Web.UI.HtmlControls.HtmlInputFile'.

I didn't realise I was converting anything.

The other error is:

Me.messageArea.Text = "Error uploading file: " + **FileUpload1**.Value & "<br>" & ex.ToString() & "<br>"

Yes, I use intellisense and it says: 'Value' is not a member of 'System.Web.UI.WebControls.FileUpload'. It also gives me some alternatives such as 'FileName' (in fact, the error disappears with 'FileName').

Thanks again for your reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.