コード例 #1
0
        public ActionResult AddOrEdit(Blog blog, HttpPostedFileBase uploadedFile)
        {
            var allowedExtensions = new[] { ".GIF", ".PNG", ".JPG", ".JPEG" };

            if (uploadedFile != null)
            {
                var ext = Path.GetExtension(uploadedFile.FileName);
                if (allowedExtensions.Contains(ext.ToUpper())) //check what type of extension
                {
                    string myfile    = "BlogImage" + DateTime.Now.ToString("ddMMyyhhmm") + ext;
                    var    path      = ConfigurationManager.AppSettings["BlogImage"];
                    var    finalpath = Path.Combine(Server.MapPath(path), myfile);
                    if (blog.BlogId > 0)
                    {
                        var imageName    = blog.BlogImage;
                        var existingpath = ConfigurationManager.AppSettings["BlogImage"];
                        if (System.IO.File.Exists(Server.MapPath(existingpath + imageName)))
                        {
                            System.IO.File.Delete(Server.MapPath(existingpath + imageName));
                        }
                    }
                    blog.BlogImage = myfile;
                    uploadedFile.SaveAs(finalpath);
                }
                else
                {
                    Message message = new Message();
                    message.ReturnMessage = "Choose only Image File!";
                    message.MessageType   = MessageTypes.Information;
                }
            }
            else
            {
                Message message = new Message();
                message.ReturnMessage = "Select an Image!";
                message.MessageType   = MessageTypes.Information;
            }
            var data = _iBlogManager.AddOrEdit(blog);

            return(Json(new { messageType = data.MessageType, message = data.ReturnMessage, html = GlobalClass.RenderRazorViewToString(this, "ViewAll", _iBlogManager.GetAllBlog()) }, JsonRequestBehavior.AllowGet));
        }