Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my application i have a file uploader which accept multiple files in a single fileupload control i need to validate the extensions of all the files in that file uploader in such a way that it should accept only doc and docx suppose if i select 5 fils in a file uploader all the files should be of -b.doc,-b.docx,-b.DOC and -b.DOCX if there are any other extensions other than these four i should show an error message and clear the files which are in that particular file uploader how can i do this below is my file uploader code.


<asp:FileUpload ID="filDoc" runat="server" multiple="multiple"/>


What I have tried:

i tried the below code the problem is it is showing alert instead it should show error message after displaying the error message it should cleat the files in file uploader but it is showing two files 

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

<script type="text/javascript">
        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;
        }
    </script>
Posted
Updated 5-Sep-17 9:34am

Your code will work only for a single file and never for multiple files...
Actually ASP.NET FileUpload is an old school control, mostly server side base, so you have no real client side functionality...
You have two choices:
1. Pick a modern - client side based - upload control
2. Use the server side capabilities of FileUpload to validate and return error message to client
 
Share this answer
 
You need to iterate over the files collection[^], in browsers that support it. (Browsers that don't support it[^] don't support multiple file selection either.)
JavaScript
function checkSingleFileExtension(fileName, validExtensions) {
    var index = fileName.lastIndexOf('.');
    if (index == -1) {
        alert('All files must have an extension.');
        return false;
    }
    
    var ext = fileName.substr(index + 1).toLowerCase();
    for (var index = 0; index < validExtensions.length; index++) {
        if (ext === validExtensions[index]) {
            return true;
        }
    }
    
    alert('The file extension ' + ext.toUpperCase() + ' is not allowed.');
    return false;
}

function checkFileExtension(elem) {
    var validExtensions = ['doc', 'pdf'];
    
    if (elem.files) {
        for (var index = 0; index < elem.files.length; index++) {
            var file = elem.files[index];
            if (!checkSingleFileExtension(file.name, validExtensions)){
                return false;
            }
        }
    }
    else {
        if (!checkSingleFileExtension(elem.value, validExtensions)){
            return false;
        }
    }
    
    return true;
}

HOWEVER, be aware that not all operating systems use file extensions. Your code will only really work on Windows. You should really be using the accept attribute instead.

<input type="file"> - HTML | MDN :: Limiting accepted file types[^]

Also, it's quite easy to turn off or otherwise bypass Javascript validation. Anything you validate on the client should also be validated on the server.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900