图片上传
方法一:
Home/Index.aspx中的代码
<% using (Html.BeginForm("up","Home",FormMethod.Post,new{enctype="multipart/form-data"})) {%> <input type="file" name="upfile" /> <input type ="submit" name ="upload" value ="上传" /> <%} %> Homecontroller中的代码 [code] [AcceptVerbs(HttpVerbs.Post)] public ActionResult up(HttpPostedFileBase upfile) { if (upfile != null) { if (upfile.ContentLength > 0) { upfile.SaveAs("d:\\7.jpg"); } } return RedirectToAction("Index"); }
方法二:
Home/Index.aspx中的代码
<form action="<%=Url.Action("upload2") %>" enctype="multipart/form-data" method="post"> <input name="up1" type="file" /><input type="submit" /> </form>
Homecontroller中的代码
public ActionResult upload2(HttpPostedFileBase up1) { up1.SaveAs("d:\\8.jpg"); return Content(up1.FileName); }
局部新代码:
public ActionResult addohoto(HttpPostedFileBase tpicture)
{
//判断图片是否为空
if (tpicture != null && tpicture.ContentLength > 0)
{
//新的图片名字
string imgName = DateTime.Now.ToString("yyyymmdd") + 自己的名字.FileName.Substring(自己的名字.FileName.LastIndexOf('.'));
//保存代码
tpicture.SaveAs(Server.MapPath("~/images/" + imgName));
//数据库中的代码
xc.tpicture = "/images/" + imgName;
}
//判断给出友好提示
if (tpbll.addimage(xc) > 0)
{
return RedirectToAction("afterlogin");
}
else
{
return Content("<script>alert('添加失败!')</script>");
}
}
浙公网安备 33010602011771号