2012-03-17 80 views
1

我正在開發一個應用程序在asp.net mvc中,我想上傳一個文件。在視圖方面,當我的視圖調用控制器時,我在運行時呈現我的上傳文件字段,它提供了FormCollection中的所有數據,並且在控制器端,我只能以字符串和請求的形式獲取上傳文件的名稱.File [0] .count等於零。現在我怎麼能解決這個問題。在Asp.net MVC中文件上傳並通過FormCollection創建文件

代碼可以按需分享。

問候

+0

參考http://stackoverflow.com/questions/9411563/asp- net-mvc3-razor-file-upload-given-zero-as-file-count爲一個相關的問題 – Lijo 2012-12-20 14:45:07

回答

2

嘗試在View添加此:

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <input type="file" id="file" name="file" /> 
    <input type="submit" value="upload" /> 
} 

而在你controller文件:

[HttpPost] 
public ActionResult Index(HttpPostedFileBase file) 
{ 
    return View(); 
}