除了 Elvin 的回答之外,您还可以使用一个模型,您可以在其中添加包含您想要允许的扩展的数据注释:http: //msdn.microsoft.com/en-us/library/ee256141%28VS.100%29.aspx
模型:CompanyModel.cs
public class CompanyModel
{
[Display(Name = "Logo")]
[File(AllowedFileExtensions = new string[] { ".jpg", ".gif", ".tiff", ".png" }, MaxContentLength = 1024 * 1024 * 30, ErrorMessage = "Invalid File")]
public HttpPostedFileBase LogoFileUp{ get; set; }
//you can add other properties if you like, for example companyname
}
风景
@model CompanyModel
@using (Html.BeginForm("Create", "Company", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationMessageFor(model => model.LogoFileUp)
@Html.LabelFor(model => model.LogoFileUp)
@Html.TextBoxFor(model => model.LogoFileUp, new { type = "file" })
}
那个行动
[HttpPost]
public ActionResult Create(CompanyModel company)
{
if (ModelState.IsValid)
{
//save company ...
}else{
return View(company)
}
}