2012-03-17 74 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(); 
}