コード例 #1
0
        public static UploadImageResult DeleteImage(HttpServerUtilityBase server, int articleId, string article_state)
        {
            string          sourceDir = string.Empty;
            DataAccessLayer datalayer = null;
            var             result    = new UploadImageResult {
                Success = false
            };

            try
            {
                sourceDir = server.MapPath("/Content/images/articles");
                string[] picList = Directory.GetFiles(sourceDir, articleId + ".*");

                foreach (string pic in picList)
                {
                    System.IO.File.Delete(pic);
                    string imagePath = string.Empty;

                    datalayer = new DataAccessLayer();
                    if (article_state == "entry")
                    {
                        datalayer.UpdateArticleEntryImage(articleId, imagePath);
                    }
                    else if (article_state == "published")
                    {
                        datalayer.UpdateArticlePublishedImage(articleId, imagePath);
                    }
                }
                result.Success = true;
            }
            catch (Exception ex)
            {
                // Throw Exception
                result.Errors.Add("Error while deleting the image.");
            }
            return(result);
        }
コード例 #2
0
        public static UploadImageResult UploadImage(HttpPostedFileBase image, HttpServerUtilityBase server, int articleId, string article_state)
        {
            var result = new UploadImageResult {
                Success = false
            };
            string path              = "/Content/images/articles/";
            int    maxSize           = 300000;
            string allowedExtensions = "jpg,png,bmp,gif,jpeg";
            string imagePath         = string.Empty;

            if (image == null || image.ContentLength == 0)
            {
                result.Errors.Add("You didn't select a image file or the file you uploaded was invalid.");
                return(result);
            }

            // Check image size
            if (image.ContentLength > maxSize)
            {
                result.Errors.Add("Image was larger than the maximum upload size.Max image size allowed is 256KB.");
            }

            // Check image extension
            var extension = Path.GetExtension(image.FileName).Substring(1).ToLower();

            if (!allowedExtensions.Contains(extension))
            {
                result.Errors.Add("Only image files can be uploaded with extension. jpg,png,bmp,gif,jpeg");
            }

            // If there are no errors save image
            if (!result.Errors.Any())
            {
                // update image to db
                var             newName    = "";
                var             serverPath = "";
                DataAccessLayer datalayer  = null;

                //add image to repository
                newName    = articleId + "." + extension;
                serverPath = server.MapPath("~" + path + newName);
                image.SaveAs(serverPath);
                result.ImagePath = "../../Content/images/articles/" + newName;

                //update image to db
                //dataAccess = new DataAccessLayer();
                imagePath = result.ImagePath;
                datalayer = new DataAccessLayer();
                if (article_state == "published")
                {
                    datalayer.UpdateArticlePublishedImage(articleId, imagePath);
                }
                else if (article_state == "entry")
                {
                    datalayer.UpdateArticleEntryImage(articleId, imagePath);
                }


                result.Success = true;
            }

            return(result);
        }