嘿... 我对我的 View 有上传控制。有没有办法将此控件与模型数据关联(例如 LabelFor 或 TextBoxFor)。我需要这个,因为在页面加载时我丢失了文件上传控件中的信息 谢谢
最佳答案
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 - MVC-- 文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4658081/