Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in my app, i restrict the user to select only excel file otherwise i want to warn the user to select excel file only using file upload control
Posted

You can use JavaScript to do so,you can use regular expression to get file extension like this:

var extens=(/[.]/.exec(myfile)) ? /[^.]+$/.exec(myfile) : undefined;


myfile is the address or the path of the file. This simply takes the part of the string after "." . Then you can place some control checks with if. Something like :

if(extens=="doc" || extens=="pdf"|| extens=="xls")
{
//your method goes here
}
else
{
alert("file is invalid");
}

You can also refer to this for more help:
http://www.coursesweb.net/javascript/check-file-type-before-upload_t[^]

hope this helps :)
 
Share this answer
 
v2
Comments
parmar_punit 18-Jun-11 8:35am    
good answer, my 5
Uday P.Singh 18-Jun-11 8:36am    
thanks :)
Srinivas Kumar Pasumarthi 18-Jun-11 8:41am    
ok , thats good ,thank u
Uday P.Singh 18-Jun-11 8:43am    
welcome,please accept the answer :)
Uday P.Singh 18-Jun-11 8:44am    
thanks for accepting!!
Another solution with Javascript and regular expretion

var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
if(ext != "xls")
{
     alert("Upload xls file only");
}


http://www.esoftcoder.com[^]
 
Share this answer
 
v4

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