Reputation: 21
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?
Upvotes: 2
Views: 2678
Reputation: 31641
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"}))
Upvotes: 17