无法将HttpFileCollectionBase转换为HttpFileCollection

uno*_*nom 2 asp.net-mvc file-upload

我有一个局部观点:

<% using (Html.BeginForm("add", "home", FormMethod.Post, 
   new { enctype = "multipart/form-data" })){%>
   <input name="IncomingFiles" type="file" />
   <div class="editor-field"><%: Html.TextBox("TagsInput") %></div>
   <p><input type="submit" value="Create" /></p><% } %>
Run Code Online (Sandbox Code Playgroud)

而这在控制器中:

    [HttpPost]
    public ActionResult add(HttpFileCollection IncomingFiles, string TagsInput)
    {
         return View();
    }
Run Code Online (Sandbox Code Playgroud)

它不会将我上传的文件与HttpFileCollection匹配,它们就是HttpFileCollectionBase.我怎样才能让视图传递给我一个HttpFileCollection?

我需要任何特定的BeginForm args吗?

谢谢!

Tah*_*aza 6

而是在你的行动方面做这样的事情.您不将文件作为参数传递:

[HttpPost]
public ActionResult add(string TagsInput) {
  if (Request.Files.Count > 0) {
    // for this example; processing just the first file
         HttpPostedFileBase file = Request.Files[0];
 if (file.ContentLength == 0) {
      // throw an error here if content length is not > 0
      // you'll probably want to do something with file.ContentType and file.FileName
      byte[] fileContent = new byte[file.ContentLength];
      file.InputStream.Read(fileContent, 0, file.ContentLength);
      // fileContent now contains the byte[] of your attachment...
    }  
  }
  return View();
}
Run Code Online (Sandbox Code Playgroud)