예제 #1
0
 public ActionResult Dis_Add_list(Distribution d)
 {
     using (DormitoryDBEntitie db = new DormitoryDBEntitie())
     {
         HttpFileCollectionBase files = Request.Files;
         HttpPostedFileBase     file  = files[0];
         //获取文件名后缀
         string extName = Path.GetExtension(file.FileName).ToLower();
         //获取保存目录的物理路径
         if (System.IO.Directory.Exists(Server.MapPath("/Images/")) == false)//如果不存在就创建images文件夹
         {
             System.IO.Directory.CreateDirectory(Server.MapPath("/Images/"));
         }
         string path = Server.MapPath("/Images/"); //path为某个文件夹的绝对路径,不要直接保存到数据库
         //    string path = "F:\\TgeoSmart\\Image\\";
         //生成新文件的名称,guid保证某一时刻内图片名唯一(文件不会被覆盖)
         string fileNewName = Guid.NewGuid().ToString();
         string ImageUrl    = path + fileNewName + extName;
         //SaveAs将文件保存到指定文件夹中
         file.SaveAs(ImageUrl);
         //此路径为相对路径,只有把相对路径保存到数据库中图片才能正确显示(不加~为相对路径)
         string url = "\\Images\\" + fileNewName + extName;
         d.DistributionImg  = fileNewName + extName;
         d.DistributionTime = Convert.ToDateTime("yyyy-MM-dd HH:mm:ss");
         db.Distribution.Add(d);
         if (db.SaveChanges() > 0)
         {
             return(Content("ok"));
         }
         else
         {
             return(Content("on"));
         }
     }
 }
예제 #2
0
 /// <summary>
 /// 删除
 /// </summary>
 /// <param name="id">需要删除行的ID</param>
 /// <returns></returns>
 public ActionResult Del(string id)
 {
     using (DormitoryDBEntitie db = new DormitoryDBEntitie())
     {
         Distribution list = db.Distribution.Find(int.Parse(id));
         db.Distribution.Remove(list);
         if (db.SaveChanges() > 0)
         {
             return(Content("ok"));
         }
         else
         {
             return(Content("no"));
         }
     }
 }