Не удается преобразовать HttpFileCollectionBase в HttpFileCollection - PullRequest
2 голосов
/ 09 июля 2010

У меня есть частичное представление:

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

А это в контроллере:

    [HttpPost]
    public ActionResult add(HttpFileCollection IncomingFiles, string TagsInput)
    {
         return View();
    }

Он просто не будет сопоставлять мой загруженный файл с HttpFileCollection, они получаются как HttpFileCollectionBase. Как я могу получить представление, чтобы передать мне HttpFileCollection?

Нужны ли какие-либо конкретные аргументы BeginForm?

Спасибо!

1 Ответ

5 голосов
/ 09 июля 2010

Сделайте что-то подобное вместо этого на вашей стороне действия.Вы не передаете файлы как параметры:

[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();
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...