MVC中上传文件图片代码
众所周知,一个文件的上传往往需要用到文件、图片类型的代码,下面我来分享上传文件图片代码
public ActionResult Upload()
{
return View();
}
[HttpPost ]
public ActionResult Upload(HttpPostedFileBase file)
{
if (file!=null )
{
if (file.ContentLength ==0)
{
//文件大小为0,返回到试图
return View();
}
else
{
string[] fileType = { "image/jpeg", "image/jpg" };
if (fileType.Contains (file.ContentType ) )
{
//保存文件
file.SaveAs(Server.MapPath("~/Img/" + file.FileName));
Response.Write("上传成功"+file.ContentType );
ViewBag.img = "/img/" + file.FileName;
}
else
{
Response.Write("图片格式不正确"+file.ContentType );
}
}
}
return View();
}
## 上面的是后台代码,下面是前台调
用代码