views:

20

answers:

1

I have a partial view:

<% 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><% } %>

And this in the controller:

    [HttpPost]
    public ActionResult add(HttpFileCollection IncomingFiles, string TagsInput)
    {

         return View();

    }

It will simply not match up my uploaded file to the HttpFileCollection, they come out as HttpFileCollectionBase. How can i get the view to pass me a HttpFileCollection?

Do i need any specific BeginForm args?

Thank you!

+1  A: 

Do something like this instead on your action side. You don't pass the files as parameters:

[HttpPost]
public ActionResult add(string TagsInput) {
  if (Request.Files.Count > 0) {
    // for this example; processing just the first file
    if (file.ContentLength == 0) {
      HttpPostedFileBase file = Request.Files[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();
}
Tahbaza