限制用户仅选择excel [英] restricting the user to select excel only

查看:123
本文介绍了限制用户仅选择excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我限制用户仅选择excel文件,否则我想警告用户仅使用文件上传控件选择excel文件.

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

推荐答案

您可以使用JavaScript来做到这一点,您可以使用正则表达式来获取文件扩展名,如下所示:

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;
/.exec(myfile) : undefined;



myfile是文件的地址或路径.这只是将字符串放在."之后. .然后,您可以使用if进行一些控制检查.像:



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");
}


您也可以参考此以获得更多帮助:
http://www.coursesweb.net/javascript/check-file-type-before-upload_t [^ ]

希望对您有所帮助:)


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

hope this helps :)


使用Javascript和常规表达式的另一种解决方案

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 [ ^ ]



http://www.esoftcoder.com[^]


这篇关于限制用户仅选择excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆