MVC 3 输入文件始终为空

Bah*_*mut 4 asp.net-mvc asp.net-mvc-3

我添加了一个输入文件字段,但它在控制器上始终为空。我缺少什么?

这是我的视图和控制器的代码。

看法:

...
@using (Html.BeginForm())
{
    ...

    <input type=file name="file" id="file" class="post-attachment" />

    ...
}
Run Code Online (Sandbox Code Playgroud)

控制器:

[HttpPost]
public ViewResult _Details(HttpPostedFileBase file, ViewTopic viewTopic, string SearchField, string submitBtn)
{
    // save file to server
    if (file != null && file.ContentLength > 0)
    {
        var fileName = DateTime.Today.ToString("yy.MM.dd") + Path.GetFileName(file.FileName);
        var path = Path.Combine(Server.MapPath("~/Attachments"), fileName);
        file.SaveAs(path);
    }

...
}
Run Code Online (Sandbox Code Playgroud)

Ada*_*gan 5

您需要显式设置表单的enctype

@using(Html.BeginForm("action", "controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    ...
}
Run Code Online (Sandbox Code Playgroud)