MVC 3文件上传和模型绑定

Fra*_*cis 91 asp.net-mvc razor asp.net-mvc-3

我有一个表单上传,但我想传递我的数据库的模型信息,以保存具有不同名称的文件.

这是我的剃刀观点:

@model CertispecWeb.Models.Container

@{
  ViewBag.Title = "AddDocuments";
}

<h2>AddDocuments</h2>

@Model.ContainerNo

@using (Html.BeginForm("Uploadfile", "Containers", FormMethod.Post, 
            new { enctype = "multipart/form-data" }))
{
    <input type='file' name='file' id='file' />
    <input type="submit" value="submit" />
}
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

[HttpPost]
public ActionResult Uploadfile(Container containers, HttpPostedFileBase file)
{
     if (file.ContentLength > 0)
     {
        var fileName = Path.GetFileName(file.FileName);
        var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"),
                       containers.ContainerNo);
        file.SaveAs(path);
     }

     return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)

模型信息不会传递给控制器​​.我已经读过我可能需要更新模型,我该怎么做?

Dar*_*rov 123

您的表单不包含除文件之外的任何输入标记,因此在您的控制器操作中,您不能期望获得除上载文件之外的任何内容(这就是发送到服务器的所有内容).实现此目的的一种方法是包含一个隐藏的标记,其中包含模型的ID,允许您从发布的控制器操作中的数据存储区中检索它(如果用户不应该修改模型,请使用此标记但只需附上一个文件):

@using (Html.BeginForm("Uploadfile", "Containers", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.HiddenFor(x => x.Id)
    <input type="file" name="file" id="file" />
    <input type="submit" value="submit" />
}
Run Code Online (Sandbox Code Playgroud)

然后在你的控制器动作中:

[HttpPost]
public ActionResult Uploadfile(int id, HttpPostedFileBase file)
{
    Containers containers = Repository.GetContainers(id);
    ...
}
Run Code Online (Sandbox Code Playgroud)

另一方面,如果您希望允许用户修改此模型,则需要为要发送到服务器的模型的每个字段包含正确的输入字段:

@using (Html.BeginForm("Uploadfile", "Containers", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.TextBoxFor(x => x.Prop1)
    @Html.TextBoxFor(x => x.Prop2)
    @Html.TextBoxFor(x => x.Prop3)
    <input type="file" name="file" id="file" />
    <input type="submit" value="submit" />
}
Run Code Online (Sandbox Code Playgroud)

然后您将使用默认模型绑定器从请求重建此模型:

[HttpPost]
public ActionResult Uploadfile(Container containers, HttpPostedFileBase file)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢,效果很好.弗朗西斯 (4认同)

isx*_*ker 8

解决了

模型

public class Book
{
public string Title {get;set;}
public string Author {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

调节器

public class BookController : Controller
{
     [HttpPost]
     public ActionResult Create(Book model, IEnumerable<HttpPostedFileBase> fileUpload)
     {
         throw new NotImplementedException();
     }
}
Run Code Online (Sandbox Code Playgroud)

和观点

@using (Html.BeginForm("Create", "Book", FormMethod.Post, new { enctype = "multipart/form-data" }))
{      
     @Html.EditorFor(m => m)

     <input type="file" name="fileUpload[0]" /><br />      
     <input type="file" name="fileUpload[1]" /><br />      
     <input type="file" name="fileUpload[2]" /><br />      

     <input type="submit" name="Submit" id="SubmitMultiply" value="Upload" />
}
Run Code Online (Sandbox Code Playgroud)

注意来自控制器操作的参数标题必须与输入元素的名称匹配 IEnumerable<HttpPostedFileBase> fileUpload- >name="fileUpload[0]"

fileUpload 必须匹配

  • 此解决方案是我找到的多个文件的唯一解决方案.感谢您分享您的代码. (2认同)

jha*_*999 6

如果您不总是将图像发布到您的操作,您可以执行以下操作:

[HttpPost]
public ActionResult Uploadfile(Container container, HttpPostedFileBase file) 
{
    //do container stuff

    if (Request.Files != null)
    {
        foreach (string requestFile in Request.Files)
        {
            HttpPostedFileBase file = Request.Files[requestFile]; 
            if (file.ContentLength > 0)
            {
                string fileName = Path.GetFileName(file.FileName);
                string directory = Server.MapPath("~/App_Data/uploads/");
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }
                string path = Path.Combine(directory, fileName);
                file.SaveAs(path);
            }
        }
    }

} 
Run Code Online (Sandbox Code Playgroud)