在Asp.net MVC中上传文件并通过FormCollection定位文件

时间:2012-03-17 06:32:23

标签: c# asp.net asp.net-mvc

我正在asp.net mvc中开发一个应用程序,我想上传一个文件。在视图方面,当我的视图调用控制器时,我在运行时渲染上传文件字段,它提供FormCollection和控制器端的所有数据,我只能以字符串和请求的形式获取上传文件的名称.File [0] .count等于零。现在我如何解决这个问题。

代码可以按需分享。

此致

1 个答案:

答案 0 :(得分: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();
}