是否有用于文件上传的HTMLHelper
?具体来说,我正在寻找替代品
<input type="file"/>
使用 ASP.NET MVC HTMLHelper。
或者,如果我使用
using (Html.BeginForm())
文件上传的HTML控件是什么?
最佳答案
HTML 上传文件 ASP MVC 3。
模型:(请注意,FileExtensionsAttribute 在 MvcFutures 中可用。它将验证文件扩展名客户端和服务器端。)
public class ViewModel
{
[Required, Microsoft.Web.Mvc.FileExtensions(Extensions = "csv",
ErrorMessage = "Specify a CSV file. (Comma-separated values)")]
public HttpPostedFileBase File { get; set; }
}
HTML View :
@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new
{ enctype = "multipart/form-data" }))
{
@Html.TextBoxFor(m => m.File, new { type = "file" })
@Html.ValidationMessageFor(m => m.File)
}
Controller 操作:
[HttpPost]
public ActionResult Action(ViewModel model)
{
if (ModelState.IsValid)
{
// Use your file here
using (MemoryStream memoryStream = new MemoryStream())
{
model.File.InputStream.CopyTo(memoryStream);
}
}
}
关于asp.net-mvc - <input type ="file"/> 的 Html 帮助器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/304617/