Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
I am using file upload dialogue box for upload image in my application. I want to set a filter on file upload dialogue box that when user open it only image format file should be visible in it no other file should appear. please help me
Posted

1 solution

Hi,

XML
<asp:FileUpload ID="FileUpload1" runat="server" />

<asp:Button ID="btnUpload" runat="server" Text="Upload" />

C#
<script type="text/javascript">

    function validateFileUpload(obj){
        var fileName = new String();
        var fileExtension = new String();

        // store the file name into the variable
        fileName = obj.value;

        // extract and store the file extension into another variable
        fileExtension = fileName.substr(fileName.length - 3, 3);

        // array of allowed file type extensions
        var validFileExtensions = new Array("jpg", "png", "gif");

        var flag = false;

        // loop over the valid file extensions to compare them with uploaded file
        for(var index = 0; index < validFileExtensions.length; index++){
            if(fileExtension.toLowerCase() == validFileExtensions[index].toString().toLowerCase()){
                flag = true;
            }
        }

        // display the alert message box according to the flag value
        if(flag == false){
            alert('Files with extension ".' + fileExtension.toUpperCase() + '" are not allowed.\n\nYou can upload the files with following extensions only:\n.jpg\n.png\n.gif\n');
            return false;
        }
        else{
            alert('File has valid extension.');
            return true;
        }
    }
</script>

C#
protected void Page_Load(object sender, EventArgs e)
{
    FileUpload1.Attributes.Add("onchange", "return validateFileUpload(this);");

    btnUpload.Attributes.Add("onclick", "return validateFileUpload(document.getElementById('" + FileUpload1.ClientID + "'));");
}
 
Share this answer
 
Comments
betu.009 24-Mar-12 3:01am    
Thanks a lot
Mohamed Mitwalli 24-Mar-12 3:16am    
ur welcome

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