protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteItem")
            {
                object primaryKey = e.CommandArgument;
                Guid   imageID    = new Guid(primaryKey.ToString());

                var item = service.FindOne(imageID);

                if (item != null)
                {
                    service.Delete(imageID);

                    if (!string.IsNullOrWhiteSpace(item.OriginalImage))
                    {
                        string fileName1 = Server.MapPath(string.Format(@"~/{0}/{1}", OriginalFolder, item.OriginalImage));
                        if (System.IO.File.Exists(fileName1))
                        {
                            System.IO.File.Delete(fileName1);
                        }
                    }
                    if (!string.IsNullOrWhiteSpace(item.CropImage))
                    {
                        string fileName2 = Server.MapPath(string.Format(@"~/{0}/{1}", CropFolder, item.CropImage));
                        if (System.IO.File.Exists(fileName2))
                        {
                            System.IO.File.Delete(fileName2);
                        }
                    }

                    ImageDataBound();
                }
            }
        }
        /// <summary>
        /// Processes the delete.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        private Dictionary <string, string> ProcessDelete(string id)
        {
            var jo = new Dictionary <string, string>();

            if (string.IsNullOrWhiteSpace(id))
            {
                jo.Add("result", "error");
                jo.Add("msg", "無ID編號");
            }
            else
            {
                var imageId = new Guid(id.ToString());

                var item = _service.FindOne(imageId);

                if (item == null)
                {
                    jo.Add("result", "error");
                    jo.Add("msg", "資料不存在");
                }
                else
                {
                    try
                    {
                        _service.Delete(imageId);

                        if (!string.IsNullOrWhiteSpace(item.OriginalImage))
                        {
                            string fileName1 = Server.MapPath(string.Format(@"~/{0}/{1}",
                                                                            OriginalFolder,
                                                                            item.OriginalImage));

                            if (System.IO.File.Exists(fileName1))
                            {
                                System.IO.File.Delete(fileName1);
                            }
                        }
                        if (!string.IsNullOrWhiteSpace(item.CropImage))
                        {
                            string fileName2 = Server.MapPath(string.Format(@"~/{0}/{1}",
                                                                            CropFolder,
                                                                            item.CropImage));

                            if (System.IO.File.Exists(fileName2))
                            {
                                System.IO.File.Delete(fileName2);
                            }
                        }
                        jo.Add("result", "OK");
                        jo.Add("msg", "");
                    }
                    catch (Exception ex)
                    {
                        jo.Add("result", "exception");
                        jo.Add("msg", ex.Message);
                    }
                }
            }
            return(jo);
        }
Пример #3
0
        private Dictionary <string, string> ProcessDelete(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                return(MiscUtility.GetErrorMsg("No id"));
            }
            else
            {
                Guid imageID = new Guid(id);
                var  item    = service.FindOne(imageID);

                if (item == null)
                {
                    return(MiscUtility.GetErrorMsg("data not found"));
                }

                try
                {
                    service.Delete(imageID);

                    if (!string.IsNullOrWhiteSpace(item.OriginalImage))
                    {
                        string fileName1 = Server.MapPath(string.Format("~/{0}/{1}", OriginalFolder, item.OriginalImage));
                        if (System.IO.File.Exists(fileName1))
                        {
                            System.IO.File.Delete(fileName1);
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(item.CropImage))
                    {
                        string fileName2 = Server.MapPath(string.Format("~/{0}/{1}", CropFolder, item.CropImage));
                        if (System.IO.File.Exists(fileName2))
                        {
                            System.IO.File.Delete(fileName2);
                        }
                    }

                    return(MiscUtility.GetSuccess());
                }
                catch (Exception ex)
                {
                    return(MiscUtility.GetExceptionMsg(ex.Message));
                }
            }
        }
Пример #4
0
        public async Task SaveImage()
        {
            var uploadPathName = "upload";
            var path           = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, uploadPathName);
            IUploadImageService imageService = new UploadImageService(uploadPathName);

            string guid = await imageService.SaveAsync(GetHttpRequest().Content);

            Assert.IsNotNull(guid);

            var newFilePath = Path.Combine(path, guid);
            var fileExist   = File.Exists(newFilePath);

            Assert.IsTrue(fileExist);

            var deleteResult = imageService.Delete(guid);

            Assert.IsTrue(deleteResult);
        }