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?
 
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
0
 
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
0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.