MVC - 文件上传

Cip*_*ipi 3 asp.net-mvc file-upload

嘿......我上传了控制权.有没有办法将此控件与模型数据(如LabelFor或TextBoxFor)相关联.我需要这个,因为在页面加载时我在文件上传控件Thx中丢失了我的信息

Pau*_*nis 8

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; }
}
Run Code Online (Sandbox Code Playgroud)

HTML视图:

@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)
}
Run Code Online (Sandbox Code Playgroud)

控制器动作:

[HttpPost]
public ActionResult Action(ViewModel model)
{
    if (ModelState.IsValid)
    {
        // Use your file here
        using (MemoryStream memoryStream = new MemoryStream())
        {
            model.File.InputStream.CopyTo(memoryStream);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)