public ActionResult Create(BlogPost post, HttpPostedFileBase image) { if (image !=null && image.ContentLength >0) { //check the file name to make sure its and image var ext = Path.GetExtension(image.FileName).ToLower(); if (ext != ".png" && ext != ".jpg" && ext != ".jpeg" && ext != ".gif" && ext != ".bmp") ModelState.AddModelError("image","Invalid Format."); } if (ModelState.IsValid) { if (image != null) { //relative server path var filePath = "/images/Blog/Posts/"; //path on physical drive on server var absPath = Server.MapPath("~" + filePath); //media Url for relative Path post.MediaURL = filePath + image.FileName; //save image image.SaveAs(Path.Combine(absPath,image.FileName)); } db.Posts.Add(post); db.SaveChanges(); return RedirectToAction("Index"); } return View(post); }
public ActionResult Create() { BlogPost post = new BlogPost(); post.Created = DateTimeOffset.Now; return View(post); }