asp.net mvc上传文件

Posted on 2015-06-20 13:59  shaobin  阅读(177)  评论(1)    收藏  举报

在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");
}

博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3