fubo fubo - 1 year ago 130
ASP.NET (C#) Question

ASP Fileupload Accept Attribute

I've tried a plenty ways to limit the ASP FileUpload by file extension. Using asp-validation, checking the file codebehind, javascript, and so on.

now i found a new way to limit the selectable files:

<asp:FileUpload ID="fuTest" runat="server" accept=".csv" />

i added the accept attribute. is this a valid way according to ASP.net? The


is rendered as

<intput type="file"

and this attribute is used without any errors and works like a charm. Is this valid an exist any restrictions i have not noticed yet?

Answer Source

Try following:

<asp:FileUpload ID="fuTest" runat="server" />
<asp:RegularExpressionValidator ID="regexValidator" runat="server" ControlToValidate="fuTest" ErrorMessage="Only csv files are allowed" ValidationExpression="(.*\.([cC][sS][vV])$)"></asp:RegularExpressionValidator>