public ActionResult EditPic(FormCollection forms) { HttpPostedFileBase a = Request.Files["images"]; if (a != null) { //获取文件类型 string fileExtension = Path.GetExtension(a.FileName); //自定义文件名(时间+唯一标识符+后缀) string fileName = DateTime.Now.ToString("yyyy-MM-dd") + Guid.NewGuid() + fileExtension; //判断是否存在需要的目录,不存在则创建 if (!Directory.Exists(Server.MapPath("~/images/uploads"))) { Directory.CreateDirectory(Server.MapPath("~/images/uploads")); } //拼接保存文件的详细路径 string savefilePath = Server.MapPath("~/images/uploads/") + fileName; a.SaveAs(savefilePath); //若扩展名不为空则判断文件是否是指定视频类型 if (fileExtension != null) { if ("(.jpg)|(.png)|(.gif)|(.bmp)".Contains(fileExtension)) { //拼接返回的Img标签 string dbsrc = "/images/uploads/" + fileName; string username = Session["username"].ToString(); bool edit = uManager.EditUsersInfo(username, dbsrc); return(edit ? Content(dbsrc) : Content("fail")); } else { return(Content("fail")); } } else { return(Content("fail")); } } else { return(Content("sourcefail")); } }