You must Sign In to post a response.
  • Category: ASP.NET

    How to validate all the files in a multiple file uploader using ASP .NET C#

    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 files 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"/>

    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 clear 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;
  • #769373

    string[] validFileTypes={"doc","docx"};
    string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
    bool isValidFile = false;
    for (int i = 0; i < validFileTypes.Length; i++)
    if (ext == "." + validFileTypes[i] )
    isValidFile = true;
    if (!isValidFile)
    Label1.ForeColor = System.Drawing.Color.Red;
    Label1.Text = "you are choose Invalid File. Please upload a File with extension " +
    string.Join(",", validFileTypes);
    Label1.ForeColor = System.Drawing.Color.Green;
    Label1.Text = "File uploaded successfully.";

    hope this will help

  • Sign In to post your comments