Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
How to make a file upload control to accept or upload only file extensions like .jpeg, .bmp, .pdf.

Thank You In Advance.
Posted
Comments
R. Giskard Reventlov 3-Mar-11 5:34am    
What have you already tried?

C#
string allowedimages = System.Configuration.ConfigurationManager.AppSettings["allowedimages"];
        type = Path.GetExtension(Uploadlogo.PostedFile.FileName);
if (allowedimages.IndexOf(type) == -1)
        {
            //error message
        }
else{//continue your work
    }


In web.config file
XML
<appsettings>
<add key="AllowedImages" value=".jpeg,.JPEG,.jpe,.JPE,.jpg,.JPG,.gif,.GIF,.bmp,.BMP,.ico,.ICO"/>
</appsettings>
 
Share this answer
 
Comments
Albin Abel 3-Mar-11 5:56am    
good answer to check the file type after uploaded. My 5
Hi
In the page load event you can assign a javascript to get the filename.

FileUpload1.Attributes.Add("onblur", "checkFileExtension(this);");



In the javascript you can check for the file name and if it is not the correct extension make the upload box blank. This javascript can be called on submit event and return false if the extension not match.

The javascript
C#
function checkFileExtension(uploadBox) {
    alert(uploadBox.value);
    var filePath = uploadBox.value;
    //Have your code here to check the file extension. If not match give an alert and make the upload box blank
    uploadBox.value = "";
}
 
Share this answer
 
Looks like a homework assignment to a group of students. I remember answering the same yesterday.

Look here for all the replies to that question:
Same question asked and answered yesterday[^]
 
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