2010-07-08 39 views
2

我有局部視圖:無法轉換HttpFileCollectionBase到HttpFileCollection

<% 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參數嗎?

謝謝!

回答

5

在你的行動方面做這樣的事情。您不通過文件作爲參數:

[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(); 
}