954,577 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Filtering the file extensions in Fileupload Control

Hi,
I am having a FileUpload control in my application. When the application is running and if i click on browse button of fileupload control, i have to view only files which are having extension ".doc" (MS Word documents) to upload. How can i achieve this one. Pls help me out on this.

nmakkena
Newbie Poster
14 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

Unfortunately you cannot set a filter for the file browser dialog, but you could add a JavaScript function that would validate the extension of the selected file when a new file is selected. Thus you can prompt the user when he makes an invalid selection.

JS function

function checkFileExtension(elem) {
        var filePath = elem.value;

        if(filePath.indexOf('.') == -1)
            return false;
        
        var validExtensions = new Array();
        var ext = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();
        //Add valid extentions in this array
        validExtensions[0] = 'doc';
        //validExtensions[1] = 'pdf';
    
        for(var i = 0; i < validExtensions.length; i++) {
            if(ext == validExtensions[i])
                return true;
        }

        alert('The file extension ' + ext.toUpperCase() + ' is not allowed!');
        return false;
    }


On your file upload Control

Aneesh_Argent
Junior Poster
104 posts since Dec 2008
Reputation Points: 16
Solved Threads: 18
 
sedgey
Junior Poster
131 posts since Jan 2007
Reputation Points: 68
Solved Threads: 9
 

Hi there

This peace of code can be handy to deal with file extensions.

Public Shared Function GetFileName(ByVal Path As String) As String
        Try
            Dim RetVal As String
            Dim dt As String
            dt = Format(Date.Today, "yyMMdd")
            RetVal = Path & "\" & dt & ".xls"
            'RetVal = Path & "\" & "test.BIL"
            If File.Exists(RetVal) = True Then
                Return RetVal
            Else
                RetVal = Nothing
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Function


Mark as solved, if it helps you

Regards,

reach_yousuf
Junior Poster
195 posts since Sep 2007
Reputation Points: 12
Solved Threads: 38
 
tjaank
Newbie Poster
8 posts since Jul 2011
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You