?
Solved

filename validation

Posted on 2006-04-07
3
Medium Priority
?
421 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:DexterJones
3 Comments
 
LVL 39

Assisted Solution

by:appari
appari earned 400 total points
ID: 16399392
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
 
LVL 23

Accepted Solution

by:
adathelad earned 1600 total points
ID: 16399403
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
 

Author Comment

by:DexterJones
ID: 16400149
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question