Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NET file
I have a File Upload Control in a form and it allows only selected file types.
I implemented this on the button_click event of the page.
In web config:
    <add key="FileSize" value="2097152"/>
    <add key="FileType" value=".txt,.xlsx,.xls,.docx,.doc,.ppt,.pptx,.pdf"/>
In btnSubmit_Click():
string[] allowExtentions = (ConfigurationManager.AppSettings["FileType"]).Split(',');
if (allowExtentions.Contains(Path.GetExtension(FileUp.PostedFile.FileName)) || Path.GetExtension(FileUp.PostedFile.FileName) == "")
    //custom code
Is there any other way to implement the file type restrictions of File Upload Control ?? Please Help
Cos, I want it to check before postback of the page.
In this implementation, it accepts the file first and later throws an error message on a label after the post back which I don't want.
Waiting for your valuable comments.
Phanindra Bitla
Posted 12-Oct-12 4:53am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

By default the FileUpload control doesn't have a property to restrict file types when the select file window is opened. However, you can check the file uploaded extension before continuing the process.
Marcus Kramer at 12-Oct-12 10:49am
OP's answer moved to comment:
Hi Marcus,

I used a RegularExpressionValidator with:

Collapse | Copy Code

and it worked for me..

thanks for the link and quick response.

RaisKazi at 12-Oct-12 12:20pm
My 5.
Marcus Kramer at 12-Oct-12 12:22pm
Thank you Rais

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

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851

Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 12 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100