在view视图里面
@using (Html.BeginForm("uploadfile", "home", FormMethod.Post, new { enctype = "multipart/form-data"}))
{
<input type="file" name="file"/>
<input type="submit" value="submit"/>
}
在controller
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
if (file==null )
{
return Content("上传的文件为空","text/plain");
}
string fileName = Request.MapPath("~/image/")+DateTime.Now.ToFileTime().ToString ()+".jpg";
try
{
file.SaveAs(fileName);
}
catch
{
return Content("上传出错","text/plain");
}
return Content("上传成功", "text/plain");
}
原文:http://www.cnblogs.com/viphuangshaobin/p/4590484.html