public ActionResult AddImage(FileModel model) { String path; String filename; if (!CheckAndImportFile(model, out path, out filename)) return View(model); if (!IsValidImage(path)) { ModelState.AddModelError("File", "Le fichier n'est pas une image valide."); Tools.DeleteFile(path); return View(model); } InsertArticleInBDD(model.BlogId, filename, (long)Tools.MediaTypes.Image, model.Caption, model.Tags); return RedirectToAction("BlogManagement", "User", new { id = model.BlogId }); }
public ActionResult AddMusic(FileModel model) { String path; String filename; if (!CheckAndImportFile(model, out path, out filename)) return View(model); if (!path.EndsWith("mp3", true, null)) { ModelState.AddModelError("File", "Le fichier n'est pas un mp3."); Tools.DeleteFile(path); return View(model); } InsertArticleInBDD(model.BlogId, filename, (long)Tools.MediaTypes.Music, model.Caption, model.Tags); return RedirectToAction("BlogManagement", "User", new { id = model.BlogId }); }
private Boolean CheckAndImportFile(FileModel model, out String path, out String filename) { path = null; filename = null; if (model == null) throw new ArgumentNullException("model"); TestBlog(model.BlogId); if (model.File == null || model.File.ContentLength < 0) return false; // get file extension String extension = Path.GetExtension(model.File.FileName); // extract only the filename //var fileName = Path.GetFileName(model.File.FileName); filename = Guid.NewGuid().ToString() + extension; // store the file inside ~/Medias/Images folder path = Path.Combine(Server.MapPath("~/Medias"), filename); try { model.File.SaveAs(path); } catch (Exception e) { throw new Exception("Impossible to save the file", e); } return true; }