Reputation: 9081
I have an ASP.Net web application in which i have to upload a file:
@using (Html.BeginForm("Uploading_validation", "Super", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="dossier" accept="*.iso, *.rar, *.zip"/>
<br />
@Html.Label("Date d'expiration")
<input type="text" id="datepicker" name="duree" />
<br />
<input type="submit" value="OK" />
}
I'd like accept just the extensions accept="*.iso, *.rar, *.zip"
, but it didn't work.
Why does this filter not work? How can i modify the code?
Upvotes: 3
Views: 14097
Reputation: 9081
this snippet seems to be acceptable by all browsers
@using (Html.BeginForm("Uploading_validation", "Super", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="dossier" accept=".rar , .zip"/>
<br />
@Html.Label("Date d'expiration")
<input type="text" id="datepicker" name="duree" />
<br />
<input type="submit" value="OK" />
}
Upvotes: 3
Reputation: 33326
You can use the FileExtensions to achieve this:
[Required, FileExtensions(Extensions=".iso,.rar,.zip", ErrorMessage="Incorrect file format")]
Add Dossier to your model to pass it back to the view and render it like this:
@Html.TextBoxFor(m => m.Dossier, new { type = "file" })
@Html.ValidationMessageFor(m => m.Dossier)
It should validate both client and server-side.
Upvotes: 9
Reputation: 5895
accept
attribute don't supported by all of browsers. You can't rely on client side and should filter files in action.
BTW, you should use this attribute this way:
accept="application/iso,application/rar,application/zip"
Upd: in other way you can validate file extension with javascript: look at sample
Upvotes: 3