Click here to Skip to main content
12,896,108 members (57,340 online)

How to Restrict File Types in File Upload Control using ASP.NET

Bitla Phanindra asked:

Open original thread

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
Tags: ASP.NET, file


When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2017
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