public JsonResult DelResourceImg(string imgID)
 {
     try
     {
         T_ResourceImg img      = dc.Set <T_ResourceImg>().Where(a => a.ID == imgID).FirstOrDefault();
         string        filepath = Server.MapPath(img.ImgUrl.Replace("..", "~"));
         if (System.IO.File.Exists(filepath))
         {
             System.IO.File.Delete(filepath);
         }
         dc.Set <T_ResourceImg>().Remove(img);
         if (dc.SaveChanges() > 0)
         {
             return(Json(ResponseResult.GetResult(ResultEnum.Success)));
         }
         else
         {
             return(Json(ResponseResult.GetResult(ResultEnum.Fail)));
         }
     }
     catch (Exception)
     {
         return(Json(ResponseResult.GetResult(ResultEnum.Exception)));
     }
 }
Example #2
0
        public void DelImg(T_ResourceImg img)
        {
            string path = Server.MapPath(img.ImgUrl.Replace("..", "~"));

            if (System.IO.File.Exists(path))
            {
                System.IO.File.Delete(path);
            }
        }
Example #3
0
        public List <string> SaveImg(string resourceID)
        {
            List <string> imgList = new List <string>();

            if (Request.Files.Count > 0)
            {
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    HttpPostedFileBase hpfb      = Request.Files[i];
                    string[]           filetypes = hpfb.ContentType.Split('/');
                    if (filetypes[1] != "jpg" &&
                        filetypes[1] != "gif" &&
                        filetypes[1] != "png" &&
                        filetypes[1] != "bmg" &&
                        filetypes[1] != "jpeg")
                    {
                        continue;
                    }
                    T_ResourceImg img = new T_ResourceImg();
                    img.ID         = Guid.NewGuid().ToString();
                    img.ResourceID = resourceID;
                    img.IsCover    = false;
                    string filename = Guid.NewGuid().ToString() + "." + filetypes[1];
                    string path     = "~/Content/Resource/" + new Regex("[\\/:*?\"<>|]").Replace(img.ResourceID, "");
                    string oldpath  = path + "/old";
                    if (Directory.Exists(Server.MapPath(path)) == false)
                    {
                        Directory.CreateDirectory(Server.MapPath(path));
                    }
                    if (Directory.Exists(Server.MapPath(oldpath)) == false)
                    {
                        Directory.CreateDirectory(Server.MapPath(oldpath));
                    }
                    string filepath    = path + "/" + filename;
                    string oldfilepath = oldpath + "/" + filename;
                    hpfb.SaveAs(Server.MapPath(oldfilepath));
                    MakeThumNail(Server.MapPath(oldfilepath), Server.MapPath(filepath), 800, 580, "HW");
                    //删除原图
                    System.IO.File.Delete(Server.MapPath(oldfilepath));
                    imgList.Add(filepath);
                    img.ImgUrl = filepath.Replace("~", "..");
                    dc.Set <T_ResourceImg>().Add(img);
                }
            }
            return(imgList);
        }
        public JsonResult SaveResourceImg(string resourceID)
        {
            try
            {
                if (Request.Files.Count <= 0 || string.IsNullOrEmpty(resourceID))
                {
                    return(Json(ResponseResult.GetResult(ResultEnum.Errorr)));
                }
                T_ResourceImg img = new T_ResourceImg();

                img.ID         = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                img.ResourceID = resourceID;
                img.IsCover    = false;
                HttpPostedFileBase hpImg     = Request.Files[0];
                string[]           filetypes = hpImg.ContentType.Split('/');
                if (filetypes[1] != "jpg" && filetypes[1] != "gif" && filetypes[1] != "png" && filetypes[1] != "bmg" && filetypes[1] != "jpeg")
                {
                    return(Json(ResponseResult.GetResult(ResultEnum.Errorr)));
                }
                Regex  regex    = new Regex("[\\/:*?\"<>|]");
                string filename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "." + filetypes[1];
                string path     = "~/Content/Resource/" + regex.Replace(img.ResourceID, "");
                if (Directory.Exists(Server.MapPath(path)) == false)
                {
                    Directory.CreateDirectory(Server.MapPath(path));
                }
                path = path + "/" + filename;
                hpImg.SaveAs(Server.MapPath(path));
                img.ImgUrl = path.Replace("~", "..");
                dc.Set <T_ResourceImg>().Add(img);
                if (dc.SaveChanges() > 0)
                {
                    return(Json(new { result = 1, img }));
                }
                else
                {
                    return(Json(ResponseResult.GetResult(ResultEnum.Fail)));
                }
            }
            catch (Exception)
            {
                return(Json(ResponseResult.GetResult(ResultEnum.Exception)));
            }
        }
 public JsonResult SetResourceCovert(string imgID, string resourceID)
 {
     try
     {
         T_ResourceImg img = dc.Set <T_ResourceImg>().Where(a => a.ID == imgID).FirstOrDefault();
         dc.Database.ExecuteSqlCommand("update T_ResourceImg set iscover=0 where ResourceID=@rid", new SqlParameter("@rid", resourceID));
         img.IsCover = true;
         if (dc.SaveChanges() > 0)
         {
             return(Json(ResponseResult.GetResult(ResultEnum.Success)));
         }
         else
         {
             return(Json(ResponseResult.GetResult(ResultEnum.Fail)));
         }
     }
     catch (Exception)
     {
         return(Json(ResponseResult.GetResult(ResultEnum.Exception)));
     }
 }