简体   繁体   中英

ASP.NET MVC3 fileupload not working

page code

<% using (Html.BeginForm())
   { %>
<fieldset>
    <legend>上传项目材料</legend>
    <input  type="file" name="File1" />
    <input  type="submit" value="上传" />
</fieldset>
<%} %>

action code

[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();
    }
}

I'm trying but Request.Files.Count==0 is true not find Files why?

Just ran into this earlier this week. Your form tag is missing the HTML attribute (enctype) to tell the server it is posting files. Here is the solution...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM