Hello

I am getting a bit mixed up with my code:

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click

        Dim validFileTypes As String() = {"bmp", "gif", "png", "jpg", "jpeg", "doc", "docx", "txt", "xls"}

        Dim ext As String = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName)

        Dim isValidFile As Boolean = False

        For i As Integer = 0 To validFileTypes.Length - 1

            If ext = "." & validFileTypes(i) Then

                isValidFile = True

                Exit For

            End If

        Next

        If Not isValidFile Then

            Label1.Text = "Invalid File. Please upload a File with extension " & _
            String.Join(",", validFileTypes)


            Try
                FileUpload1.SaveAs("C:\Uploads\" & _
                     FileUpload1.FileName)
                Label1.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

                Label1.Text = "ERROR: " & ex.Message.ToString()

            End Try

        End If

        If Not isValidFile Then

            Label1.Text = "Invalid File. Please upload a File with extension " & _
            String.Join(",", validFileTypes)

        Else

            Label1.Text = "You have not specified a file."

        End If

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

    End Sub

When I load the page in my browser and do not upload any file, it does not say 'You have not specified a file', it says: 'Invalid File. Please upload a File with extension bmp,gif,png,jpg,jpeg,doc,docx,txt,xls', and if I upload a file correctly (let's say a txt file), I get the incorrect message: 'You have not specified a file'.

If I upload an ASP file, which is not allowed, I get the correct message displayed: 'Invalid File. Please upload a File with extension bmp,gif,png,jpg,jpeg,doc,docx,txt,xls'.

Thanks for any help.

Bluenose

Your last IF statement checks for the status of IsValidFile and if is true (line 52) you direct the code into displaying "You have not specified a file", which is obviously incorrect as IsValidFile is true;
If you haven't uploaded a file then IsValidFile must be false so it is correctly, by your code, displaying the 'Invalid file' message.
Basically, your loop construction around IsValidFile is incorrect.

Here is something to think about...try to rewrite your code so the logic looks more like this...

 If there is a file uploaded?
        // yes
        Is the file a valid type of file?
            // yes
            process the file
            // No
            Inform the user the file type is invalide
            End     
        //No
        Inform the user there is no file to upload
        End

Thanks for your replies.

I have now commented out some rubbish and have this:

End Try

        End If

        'If Not isValidFile Then

        '    Label1.Text = "Invalid File. Please upload a File with extension " & _
        '  String.Join(",", validFileTypes)

        'Else

        'End If

        Label1.Text = "You have not specified a file."

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

    End Sub

Now when I click on 'Submit' with no file selected, I get: 'You have not specified a file', which is correct.

Now I need to show 'Invalid File. Please upload a File with extension " & _' when the user tries to upload an MP3 or ASP, etc, file.

Whatever I do with this code:

 Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click

        Dim validFileTypes As String() = {"bmp", "gif", "png", "jpg", "jpeg", "doc", "docx", "txt", "xls"}

        Dim ext As String = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName)

        Dim isValidFile As Boolean = False

        For i As Integer = 0 To validFileTypes.Length - 1

            If ext = "." & validFileTypes(i) Then

                isValidFile = True

                Exit For

            End If

        Next

        If Not isValidFile Then

            Label1.Text = "Invalid File. Please upload a File with extension " & _
             String.Join(",", validFileTypes)

            Try
                FileUpload1.SaveAs("C:\Uploads\" & _
                FileUpload1.FileName)
                Label1.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

                Label1.Text = "ERROR: " & ex.Message.ToString()

            End Try

        End If

        Label1.Text = "You have not specified a file."

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

    End Sub

it only responds to that final Label1.Text in terms of the message that appears on screen. If I remove it and replace it with

Label1.Text = "Invalid File. Please upload a File with extension " & _
                 String.Join(",", validFileTypes)

then that is the message that appears on screen whether a valid file has been uploaded or not.

What a headache!

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.