如何按文件类型过滤 [英] How to Filter by file type

查看:231
本文介绍了如何按文件类型过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个FileUpload控件如何只对excel文件进​​行过滤?现在它有选项All Files(*。*),我只需要像(.xls)和(.xlsx)



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:FileUpload ID="FileUpload1" runat="server" />

推荐答案

您可以在以下链接中找到您的解决方案..

http://stackoverflow.com/questions/2506821/filter-the-file-type-with-文件上传控件 [ ^ ]

http://forums.asp.net/t/1156963.aspx?如何+ +过滤+文件+ +文件+上传+ HTML +控制 [ ^ ]
you find your solution on below links..
http://stackoverflow.com/questions/2506821/filter-the-file-type-with-the-file-upload-control[^]
http://forums.asp.net/t/1156963.aspx?How+to+filter+files+in+file+upload+HTML+control[^]


没有直接的方法。但你可以调用一个 JavaScript 函数 onchange 事件,并在该事件中验证扩展名。



参考 - FileUpload - 过滤文件类型/文件扩展名/文件大小 [ ^ ]



您也可以尝试这种方法 [ ^ ],建议使用 RegularExpressionValidator
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.


试试这个。 :)



try this. :)

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

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

           }
           else
           {

               //Select Excel File only
           }


这篇关于如何按文件类型过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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