Click here to Skip to main content
15,888,270 members
Please Sign up or sign in to vote.
2.78/5 (3 votes)
So I have a FileUpload control how to make it filter only for excel files? Now it has option All Files (*.*) and I need to be like (.xls) and (.xlsx) only

ASP.NET
<asp:FileUpload ID="FileUpload1" runat="server" />
Posted
Comments
Thanks7872 16-Apr-14 7:55am    
Do you have access to google.com?
[no name] 16-Apr-14 8:19am    
Yes, do you have a solution for my question?

 
Share this answer
 
Comments
[no name] 16-Apr-14 10:27am    
Yes this validate but I do not want validation what I want is to Filter the File Type in the dialog box to .xls & .xlsx only
[no name] 17-Apr-14 2:28am    
If you like my question please up vote it.
There is no direct method. But you can call one JavaScript function onchange event and inside that event validate the extension.

Refer - FileUpload - Filter File Type/File Extension/File Size[^]

You can also try this method[^], which suggests to use RegularExpressionValidator.
 
Share this answer
 
Comments
[no name] 16-Apr-14 10:27am    
Yes this validate but I do not want validation what I want is to Filter the File Type in the dialog box to .xls & .xlsx only
There is no other way out for restricting file types. So, the only option is to validate.
Do that.

"Everything is not achievable, sometimes you have to implement a workaround." - by Me :P
[no name] 17-Apr-14 2:27am    
OK. If you like my question please up vote it.
I am up-voting it. :)
Please accept the answer. :)
try this. :)

C#
string NameString = Path.GetExtension(FileUpload1.FileName);

           if (NameString == ".xls" || NameString == ".xlsx")
           {
               //Your code

           }
           else
           {

               //Select Excel File only
           }
 
Share this answer
 
Comments
[no name] 16-Apr-14 10:27am    
Yes this validate but I do not want validation what I want is to Filter the File Type in the dialog box to .xls & .xlsx only
[no name] 17-Apr-14 2:28am    
If you like my question please up vote it.

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