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