public ActionResult Upload(Image image, HttpPostedFileBase file) { if (!ModelState.IsValid) return View(image); if (file == null || file.ContentLength == 0) { ViewBag.error = "Please choose a file"; return View(image); } this._imageManager.Add(image, file, Server.MapPath("~")); return RedirectToAction("Index"); }
public void Add(Image image, HttpPostedFileBase file, string serverPath) { var fileName = Guid.NewGuid().ToString(); var extension = System.IO.Path.GetExtension(file.FileName).ToLower(); image.Path = AppConfiguration.ImagePath + fileName + extension; image.UploadDate = DateTime.Now; using (var img = System.Drawing.Image.FromStream(file.InputStream)) { using (var newImg = new Bitmap(img)) { newImg.Save(serverPath + image.Path, img.RawFormat); } } this._imageRepository.Add(image); }
public ActionResult Upload() { var image = new Image(); return View(image); }