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.

Recommended Answers

All 4 Replies

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

<asp:FileUpload ID="FileUpload1" runat="server" onchange ="checkFileExtension(this);" />

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,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.