ASP.NET MVC3 fileupload无法正常工作

ins*_*fom 2 asp.net file-upload asp.net-mvc-3

页面代码

<% using (Html.BeginForm())
   { %>
<fieldset>
    <legend>??????</legend>
    <input  type="file" name="File1" />
    <input  type="submit" value="??" />
</fieldset>
<%} %>
Run Code Online (Sandbox Code Playgroud)

行动代码

[HttpPost]
public ActionResult FileUpLoad(int id, FormCollection form)
{
    try
    {
        var model = db.ProjcetDeclare.First(c => c.id == id);

        if (Request.Files.Count==0)
        {
            return View();
        }
        string newFile=string.Empty;

        var File1 = Request.Files[0];
        if (File1.ContentLength == 0)
        {
        }
        newFile = model.Project.pname + DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetFileName(File1.FileName);
        File1.SaveAs(Server.MapPath("/????/" + newFile));

        model.XMCL = "/????/" + newFile;
        UpdateModel(model);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试但是Request.Files.Count == 0是真的没找到文件为什么?

Sli*_*SFT 17

本周早些时候遇到了这个问题.您的表单标记缺少HTML属性(enctype)来告诉服务器它正在发布文件.这是解决方案......

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