如何使用ASP.NET限制文件上载控件中的文件类型 [英] How to Restrict File Types in File Upload Control using 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屋!