ASP.NET - Limit file upload available file types

2019-01-21 23:11发布

I have added a file upload to my asp.net website. However, I want to limit the file types that user can select. For example, I only the user to select mp3 files. How can I add a filter to the file upload so that it displays only the mp3 files in the folder selected?

 <asp:FileUpload ID="FileUpload1" runat="server" />
 <asp:Button ID="btnAudUpload" Text="Upload" CssClass="btncssUpload" OnClick="btnAudUpload_Click" runat="server" />

10条回答
我命由我不由天
2楼-- · 2019-01-21 23:44

This is probably a very old topic, however if anyone else has this question I found that this worked for me

because asp:FileUpload converts to a html tag on client side, it logically makes sence that you can add html tags aswell.

It worked for me, now you can only select those file tipes and don't need the regular expressions

查看更多
老娘就宠你
3楼-- · 2019-01-21 23:47

There is no problem. Here it is!

<asp:FileUpload ID="FileUpload1" runat="server" accept=".mp3"/>
查看更多
看我几分像从前
4楼-- · 2019-01-21 23:49

Using RegularExpressionValidator may help you. No serverside code is necessary for the checking of the file extension. Check out this code

<asp:RegularExpressionValidator ID="uplValidator" runat="server" ControlToValidate="FileUpload1"
 ErrorMessage=".mp3, .mp4 & wma formats are allowed" 
 ValidationExpression="(.+\.([Mm][Pp][3])|.+\.([Mm][Pp][4])|.+\.([Ww][Mm][Aa]))"></asp:RegularExpressionValidator>

Remember all you have to do is now add a fileupload control with the id FileUpload1. Done. You can press F5 and see the effect

查看更多
叛逆
5楼-- · 2019-01-21 23:50

It's not possible with the FileUpload control in ASP.NET, but the following link may help:

http://forums.asp.net/p/1136820/1817938.aspx

查看更多
登录 后发表回答