如何使用ASP.NET限制文件上载控件中的文件类型 [英] How to Restrict File Types in File Upload Control using ASP.NET

查看:76
本文介绍了如何使用ASP.NET限制文件上载控件中的文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个形式的文件上载控件,它仅允许选择的文件类型.
我在页面的 button_click 事件中实现了此功能.

网络配置中:

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>



btnSubmit_Click()中:



In btnSubmit_Click():

string[] allowExtentions = (ConfigurationManager.AppSettings["FileType"]).Split(',');
if (allowExtentions.Contains(Path.GetExtension(FileUp.PostedFile.FileName)) || Path.GetExtension(FileUp.PostedFile.FileName) == "")
{
    //custom code
}



是否有其他方法可以实现文件上载控件的文件类型限制?请帮助
Cos,我希望它在页面的回发前检查.
在此实现中,它首先接受文件,然后在我不希望发回的帖子之后在标签上引发错误消息.

等待您的宝贵意见.

谢谢,
Phanindra Bitla



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

推荐答案

默认情况下,当打开选择文件窗口时,FileUpload控件没有属性来限制文件类型.但是,您可以在继续操作之前检查文件上传的扩展名.

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


这篇关于如何使用ASP.NET限制文件上载控件中的文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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