filename validation

hi,

Please kindly assist on the code needed, how can we have the files being uploaded validate that only .txt and .xls can be uploaded, otherwise display in a label object, "only txt and xls files are allowede to be uploaded".


Thanks.



    Private Function UploadFiles() As Boolean
        Try
            Dim strUploadPath As String
            Dim Loop1 As Integer
            Dim TempFileName As String
            Dim strTime As String
            Dim MyFileCollection As HttpFileCollection = Request.Files
            Dim strFileType As String
            Dim ErrScript As System.Text.StringBuilder = New System.Text.StringBuilder("")
            Dim blnErrorFlag As Boolean
            Dim strMsgPrefix As String
            For Loop1 = 1 To MyFileCollection.Count
                If MyFileCollection(Loop1 - 1).FileName.Trim <> "" Then
                    blnErrorFlag = False
                    TempFileName = System.IO.Path.GetFileName(MyFileCollection(Loop1 - 1).FileName)
                    strUploadPath = "./upload/"
                    TempFileName = Server.MapPath(strUploadPath) & TempFileName
                    MyFileCollection(Loop1 - 1).SaveAs(TempFileName)
                End If
            Next Loop1
            Return True
        Catch exc As Exception
            Throw
        End Try
    End Function

    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            UploadFiles()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub
DexterJonesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

appariCommented:
try this


 Private Function UploadFiles() As Boolean
        Try
            Dim strUploadPath As String
            Dim Loop1 As Integer
            Dim TempFileName As String
            Dim strTime As String
            Dim MyFileCollection As HttpFileCollection = Request.Files
            Dim strFileType As String
            Dim ErrScript As System.Text.StringBuilder = New System.Text.StringBuilder("")
            Dim blnErrorFlag As Boolean
            Dim strMsgPrefix As String
            For Loop1 = 1 To MyFileCollection.Count
                If MyFileCollection(Loop1 - 1).FileName.Trim <> "" Then
                    blnErrorFlag = False
                    dim sExt  as string
                    sExt = system.IO.Path.GetExtension(MyFileCollection(Loop1 - 1).FileName).ToLower()
                     if sExt.equals("txt") orelse sExt.equals("xls") then
                    TempFileName = System.IO.Path.GetFileName(MyFileCollection(Loop1 - 1).FileName)
                    strUploadPath = "./upload/"
                    TempFileName = Server.MapPath(strUploadPath) & TempFileName
                    MyFileCollection(Loop1 - 1).SaveAs(TempFileName)
                     else
'lblError.text="only txt and xls files are allowede to be uploaded".
response.write "only txt and xls files are allowede to be uploaded".



                    endif
                End If
            Next Loop1
            Return True
        Catch exc As Exception
            Throw
        End Try
    End Function
adatheladCommented:
Hi,

Try using this check inside your For loop:

If Not (MyFileCollection(Loop1-1).FileName.EndsWith(".txt") Or MyFileCollection(Loop1-1).FileName.EndsWith(".xls")) Then
    throw new Exception("Invalid file extension")
End If

Then, in your catch block, set the exception message to appear in your label:
lblYourLabel.text = ex.message

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DexterJonesAuthor Commented:
appari,

Seems there's a condition problem, I tried uploading a txt file, the page displayed:

only txt and xls files are allowede to be uploaded.

Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.