ASP.NET MVC两个文件上传,不同的目的地

rex*_*ror 2 asp.net-mvc file-upload

我正在尝试将两个不同的文件上传到同一表单上的两个不同的数据库字段中.

------------------------------------
reportid | name | image | template |
------------------------------------
Run Code Online (Sandbox Code Playgroud)

这是桌子看.所以我想将文件上传到图像和模板.我的模特:

public class Report
{
    [Key]
    public int ReportID { get; set; }

    [Required]
    public string Name { get; set; }

    public byte[] Image { get; set; }

    public byte[] Template { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

我在控制器中的Create方法:

public ActionResult Create(Report report, HttpPostedFileBase file, HttpPostedFileBase temp)
    {
        if (ModelState.IsValid)
        {
            if (file != null && file.ContentLength > 0)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    file.InputStream.CopyTo(ms);
                    report.Image = ms.GetBuffer();
                }
            }

            if (temp != null && temp.ContentLength > 0)
            {
                using (MemoryStream ms1 = new MemoryStream())
                {
                    temp.InputStream.CopyTo(ms1);
                    report.Template = ms1.GetBuffer();
                }
            }
            db.Reports.Add(report);
            db.SaveChanges();
            db.Configuration.ValidateOnSaveEnabled = true;
            return RedirectToAction("Index");  
        }
Run Code Online (Sandbox Code Playgroud)

关于上传的部分观点:

<div class="editor-label">
   <%:Html.LabelFor(model => model.Image) %>
</div>
<div class="editor-field">
   <input type="file" id="fuImage" name="file" />
</div>
<div class="editor-label">
   <%:Html.Label("Template") %>
</div>
<div class="editor-field"> 
   <input type="file" id="temp" name="temp"/>
</div>
<p>
   <input type="submit" value="Create" />
</p>
Run Code Online (Sandbox Code Playgroud)

因为我不能IEnumerable<HttpPostedFileBase> filesCreate方法中使用它作为参数,所以我完全陷入困境,因为我需要将它保存在不同的字段中,或者我可以吗?我该怎么做呢?请帮忙:S

注意:图片上传工作正常.

Shy*_*yju 5

为什么不用IEnumerable<HttpPostedFileBase>?你可以像这样使用它.

[HttpPost] 
public ActionResult Create(Report report, IEnumerable<HttpPostedFileBase> files)
{
  if (ModelState.IsValid)
  {
    //Let's take first file
     if(files.ElementAt(0)!=null)
     { 
       var file1=files.ElementAt(0);
       if (file1!= null && file1.ContentLength > 0)
       {
          //do processing of first file
       }
     }

     //Let's take the second one now.
     if(files.ElementAt(1)!=null)
     {
       var temp =files.ElementAt(1);
       if (temp!= null && temp.ContentLength > 0)
       {
          //do processing of second file here
       }
     }
  }
  //Do your code for saving the data. 
  return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)

编辑:在您的编辑中看到您的视图标记后.

文件输入元素的名称应与操作方法中的参数名称相同.(files在这个例子中)

@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  <b>File 1</b>   
  <input type="file" name="files" id="file1" />
  <b>File 2</b>
  <input type="file" name="files" id="file2" />
  <input type="submit"  />
}
Run Code Online (Sandbox Code Playgroud)

此代码假定只读取集合中的前2个条目.因为您只需要2个文件,我对索引进行了硬编码.

菲尔有一篇很好的博客文章很好地解释了它.