Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.NETfiletype
Hi,
 
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:
<appSettings>
    <add key="FileSize" value="2097152"/>
    <add key="FileType" value=".txt,.xlsx,.xls,.docx,.doc,.ppt,.pptx,.pdf"/>
  </appSettings>
 
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.
 
Thanks,
Phanindra Bitla
Posted 12 Oct '12 - 3:53


1 solution

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.
 
Source[^]
  Permalink  
Comments
Marcus Kramer - 12 Oct '12 - 10:49
OP's answer moved to comment: Hi Marcus, I used a RegularExpressionValidator with: Collapse | Copy Code ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.txt|.TXT|.xls|.XLS|.xlsx|.XLSX|.doc|.DOC|.docx|.DOCX|.pdf|.PDF|.ppt|.PPT|.pptx|.PPTX)$" and it worked for me.. thanks for the link and quick response. Regards, Phanindra
RaisKazi - 12 Oct '12 - 12:20
My 5.
Marcus Kramer - 12 Oct '12 - 12:22
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
Your Filters
Interested
Ignored
     
0 Arun Vasu 339
1 OriginalGriff 320
2 Sergey Alexandrovich Kryukov 315
3 Tadit Dash 221
4 CPallini 178
0 Sergey Alexandrovich Kryukov 9,955
1 OriginalGriff 7,589
2 CPallini 4,028
3 Rohan Leuva 3,422
4 Maciej Los 2,949


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 12 Oct 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid