2

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?

1 Answer 1

17

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"}))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.