public ActionResult AddFiles(StaticPageViewModel viewModel, IEnumerable<HttpPostedFileBase> files) { if (ModelState.IsValid) { var fileKeys = Request.Files.AllKeys; var page = staticPagesRepository.GetById(viewModel.Id); foreach (var key in fileKeys) { if (Request.Files[key] == null || Request.Files[key].ContentLength == 0) continue; using (new UnitOfWork(_currentContext)) { var file = Request.Files[key]; //upload file first var fileName = file.FileName; var absolutePath = Server.MapPath("~/Content/uploads/staticpages/" + viewModel.Id.ToString(CultureInfo.InvariantCulture)); var fullVirtualPath = "~/Content/uploads/staticpages/" + viewModel.Id.ToString(CultureInfo.InvariantCulture) + "/" + fileName; var directory = Server.MapPath("~/Content/uploads/staticpages/" + viewModel.Id.ToString(CultureInfo.InvariantCulture)); if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); //create a temp file first, then compress it FileUploader.UploadFile(file, fileName, absolutePath); var uploadedFile = new File() { FileName = fullVirtualPath, FilePurposeId = (int) FilePurposes.StaticPage }; page.Files.Add(uploadedFile); } } TempData[Const.ActionResultInfo] = "Файлы успешно прикреплены к странице"; return RedirectToAction("StaticPageFiles", new {id = page.Id}); } TempData[Const.ActionErrorInfo] = "Возникла ошибка загрузки файлов. Попробуйте позже"; return View(viewModel); }
public ActionResult EditNew(ArticleViewModel article, HttpPostedFileBase file) { if (ModelState.IsValid) { using (new UnitOfWork(_currentContext)) { var existingArticle = articlesRepository.GetById(article.Id); if (file != null) { //upload file first var ticks = DateTime.Now.Ticks; var fileName = String.Format("slideshow-{0}", ticks) + Path.GetExtension(file.FileName); var absoluteTempPath = Server.MapPath("~/Content/images/uploads/temp/"); var absolutePath = Server.MapPath("~/Content/images/uploads/news/"); var fullVirtualPath = "~/Content/images/uploads/news/" + fileName; //create a temp file first, then compress it FileUploader.UploadFile(file, fileName, absoluteTempPath); var encoder = new ImageEncoder(); encoder.Compress(absoluteTempPath + fileName, absolutePath + fileName, 620, 465); //after compressing deleting original file FileUploader.DeleteFile(absoluteTempPath + fileName); var articleImage = new File() { FileName = fullVirtualPath, FilePurposeId = (int)FilePurposes.Slideshow }; //delete the old file FileUploader.DeleteFile(Server.MapPath(existingArticle.File.FileName)); filesRepository.Remove(existingArticle.FileId); existingArticle.File = articleImage; } existingArticle.Text = article.Text; existingArticle.Title = article.Title; existingArticle.PostedAt = DateTime.Now; existingArticle.Account = accountRepository.GetByUsername(CurrentUserName); } TempData[Const.ActionResultInfo] = "Новость успешно отредактирована"; return RedirectToAction("News"); } TempData[Const.ActionErrorInfo] = "Невозможно отредактировать новость, проверьте правильность заполнения полей"; return View(); }
public ActionResult EditSlide(SlideshowViewModel slideshow, HttpPostedFileBase file) { if(ModelState.IsValid) { using(new UnitOfWork(_currentContext)) { var exising = slideshowRepository.GetById(slideshow.Id.Value); if (file != null) { //upload file first var ticks = DateTime.Now.Ticks; var fileName = String.Format("slideshow-{0}", ticks) + Path.GetExtension(file.FileName); var absoluteTempPath = Server.MapPath("~/Content/images/uploads/temp/"); var absolutePath = Server.MapPath("~/Content/images/uploads/slideshow/"); var fullVirtualPath = "~/Content/images/uploads/slideshow/" + fileName; //create a temp file first, then compress it FileUploader.UploadFile(file, fileName, absoluteTempPath); var encoder = new ImageEncoder(); encoder.Compress(absoluteTempPath + fileName, absolutePath + fileName, 620, 465); //after compressing deleting original file FileUploader.DeleteFile(absoluteTempPath + fileName); var slideshowImage = new File() { FileName = fullVirtualPath, FilePurposeId = (int) FilePurposes.Slideshow }; //delete the old file FileUploader.DeleteFile(Server.MapPath(exising.File.FileName)); filesRepository.Remove(exising.FileId); exising.File = slideshowImage; } exising.Title = slideshow.Title; exising.Text = slideshow.Text; exising.Date = DateTime.Now; } TempData[Const.ActionResultInfo] = "Слайд успешно отредактирован"; return RedirectToAction("Slideshow"); } TempData[Const.ActionErrorInfo] = "Ошибка редактирования слайда. Проверьте правильность введенных данных"; return View(slideshow); }
public ActionResult EditImage(GalleryItemViewModel gvm, HttpPostedFileBase file) { if(ModelState.IsValid) { using (new UnitOfWork(_currentContext)) { var galleryItem = galleryRepository.GetById(gvm.Id.Value); //upload file first if (file != null) { var ticks = DateTime.Now.Ticks; var fileName = String.Format("gallery-{0}", ticks) + Path.GetExtension(file.FileName); var absoluteTempPath = Server.MapPath("~/Content/images/uploads/temp/"); var absolutePath = Server.MapPath("~/Content/images/uploads/gallery/"); var fullVirtualPath = "~/Content/images/uploads/gallery/" + fileName; var absoluteThumbnailPath = Server.MapPath("~/Content/images/uploads/gallery/thumbnails/"); var fullVirtualThumbnailPath = "~/Content/images/uploads/gallery/thumbnails/" + fileName; //create a temp file first, then compress it FileUploader.UploadFile(file, fileName, absoluteTempPath); var encoder = new ImageEncoder(); encoder.Compress(absoluteTempPath + fileName, absolutePath + fileName, 800, 600); encoder.Compress(absoluteTempPath + fileName, absoluteThumbnailPath + fileName, 290, 217); //after compressing deleting original file FileUploader.DeleteFile(absoluteTempPath + fileName); var fullImage = new File() { FileName = fullVirtualPath, FilePurposeId = (int) FilePurposes.Gallery }; var thumbnailImage = new File() { FileName = fullVirtualThumbnailPath, FilePurposeId = (int) FilePurposes.Gallery }; //delete old files FileUploader.DeleteFile(Server.MapPath(galleryItem.File.FileName)); FileUploader.DeleteFile(Server.MapPath(galleryItem.Thumbnail.FileName)); filesRepository.Remove(galleryItem.FileId); filesRepository.Remove(galleryItem.ThumbnailFileId); galleryItem.File = fullImage; galleryItem.Thumbnail = thumbnailImage; } galleryItem.Title = gvm.Title; galleryItem.Text = gvm.Text; galleryItem.PostedAt = DateTime.Now; galleryItem.Account = accountRepository.GetByUsername(CurrentUserName); } TempData[Const.ActionResultInfo] = "Изображение отредактировано"; return RedirectToAction("Album", new { id = gvm.AlbumId}); } TempData[Const.ActionErrorInfo] = "Ошибка редактирования изображения. Проверьте корректность введенных данных"; return View(gvm); }
public ActionResult EditLecturer(LecturerViewModel lecturer, HttpPostedFileBase file) { if (ModelState.IsValid) { using (new UnitOfWork(_currentContext)) { var existing = lecturersRepository.GetById(lecturer.Id); File photo = null; //save photo if (file != null) { //delete original file first (if exists) if (existing.Photo != null) { FileUploader.DeleteFile(Server.MapPath("~/" + existing.Photo.FileName)); filesRepository.Remove(existing.Photo.Id); } var ticks = DateTime.Now.Ticks; var fileName = String.Format("profile-{0}", ticks) + Path.GetExtension(file.FileName); var absoluteTempPath = Server.MapPath("~/Content/images/uploads/temp/"); var absolutePath = Server.MapPath("~/Content/images/uploads/lecturers/"); var fullVirtualPath = "~/Content/images/uploads/lecturers/" + fileName; //create a temp file first, then compress it FileUploader.UploadFile(file, fileName, absoluteTempPath); var encoder = new ImageEncoder(); encoder.Compress(absoluteTempPath + fileName, absolutePath + fileName, 220, 220); //after compressing deleting original file FileUploader.DeleteFile(absoluteTempPath + fileName); photo = new File() { FileName = fullVirtualPath, FilePurposeId = (int)FilePurposes.Profile }; } //additional info DateTime? birthDate; try { birthDate = DateTime.Parse(lecturer.BirthDate); } catch { birthDate = null; } existing.Name = lecturer.Name; existing.Description = lecturer.Description; existing.Account = accountRepository.GetByUsername(CurrentUserName); existing.CommitteeId = lecturer.CommitteeId; existing.Profile.BirthDate = birthDate; existing.Profile.Degree = lecturer.Degree; existing.Profile.Email = lecturer.Email; existing.Profile.Post = lecturer.Post; existing.Profile.Room = lecturer.Room; existing.Profile.LecturerCategoryId = lecturer.LecturerCategoryId; existing.Profile.RecordOfService = lecturer.RecordOfService; existing.Photo = photo; } TempData[Const.ActionResultInfo] = "Профиль преподавателя успешно изменен"; return RedirectToAction("Lecturers"); } TempData[Const.ActionErrorInfo] = "Невозможно отредактировать профиль преподавателя. Проверьте правильность введенных данных."; return View(); }
public ActionResult AddSlide(SlideshowViewModel slideshow, HttpPostedFileBase file) { if(ModelState.IsValid) { using (new UnitOfWork(_currentContext)) { //upload file first var ticks = DateTime.Now.Ticks; var fileName = String.Format("slideshow-{0}", ticks) + Path.GetExtension(file.FileName); var absoluteTempPath = Server.MapPath("~/Content/images/uploads/temp/"); var absolutePath = Server.MapPath("~/Content/images/uploads/slideshow/"); var fullVirtualPath = "~/Content/images/uploads/slideshow/" + fileName; //create a temp file first, then compress it FileUploader.UploadFile(file, fileName, absoluteTempPath); var encoder = new ImageEncoder(); encoder.Compress(absoluteTempPath + fileName, absolutePath + fileName, 620, 465); //after compressing deleting original file FileUploader.DeleteFile(absoluteTempPath + fileName); var slideshowImage = new File() { FileName = fullVirtualPath, FilePurposeId = (int) FilePurposes.Slideshow }; var slideshowItem = new SlideshowItem { Title = slideshow.Title, Text = slideshow.Text, Date = DateTime.Now, File = slideshowImage }; slideshowRepository.Add(slideshowItem); } TempData[Const.ActionResultInfo] = "Слайд успешно добавлен"; return RedirectToAction("Slideshow"); } TempData[Const.ActionErrorInfo] = "Невозможно добавить слайд. Проверьте правильность введенных данных"; return View(slideshow); }
public ActionResult AddNew(AddArticleViewModel avm, HttpPostedFileBase file) { if (ModelState.IsValid) { using (new UnitOfWork(_currentContext)) { //upload file first var ticks = DateTime.Now.Ticks; var fileName = String.Format("article-{0}", ticks) + Path.GetExtension(file.FileName); var absoluteTempPath = Server.MapPath("~/Content/images/uploads/temp/"); var absolutePath = Server.MapPath("~/Content/images/uploads/news/"); var fullVirtualPath = "~/Content/images/uploads/news/" + fileName; //create a temp file first, then compress it FileUploader.UploadFile(file, fileName, absoluteTempPath); var encoder = new ImageEncoder(); encoder.Compress(absoluteTempPath + fileName, absolutePath + fileName, 620, 465); //after compressing deleting original file FileUploader.DeleteFile(absoluteTempPath + fileName); var articleImage = new File() { FileName = fullVirtualPath, FilePurposeId = (int)FilePurposes.Slideshow }; var article = new Article() { Account = accountRepository.GetByUsername(CurrentUserName), Title = avm.Title, Text = avm.Text, PostedAt = DateTime.Now, File = articleImage }; articlesRepository.Add(article); } TempData[Const.ActionResultInfo] = "Статья успешно добавлена"; return RedirectToAction("News"); } TempData[Const.ActionErrorInfo] = "Невозможно добавить статью, проверьте корректность введенных данных"; return View(); }
public ActionResult AddLecturer(LecturerViewModel lecturer, HttpPostedFileBase file) { if (ModelState.IsValid) { using (new UnitOfWork(_currentContext)) { File photo = null; //save photo if (file != null) { var ticks = DateTime.Now.Ticks; var fileName = String.Format("profile-{0}", ticks) + Path.GetExtension(file.FileName); var absoluteTempPath = Server.MapPath("~/Content/images/uploads/temp/"); var absolutePath = Server.MapPath("~/Content/images/uploads/lecturers/"); var fullVirtualPath = "~/Content/images/uploads/lecturers/" + fileName; //create a temp file first, then compress it FileUploader.UploadFile(file, fileName, absoluteTempPath); var encoder = new ImageEncoder(); encoder.Compress(absoluteTempPath + fileName, absolutePath + fileName, 220, 220); //after compressing deleting original file FileUploader.DeleteFile(absoluteTempPath + fileName); photo = new File() { FileName = fullVirtualPath, FilePurposeId = (int)FilePurposes.Profile }; } //additional info DateTime? birthDate; try { birthDate = DateTime.Parse(lecturer.BirthDate); } catch { birthDate = null; } var lecturerEntry = new Lecturer { Name = lecturer.Name, Description = lecturer.Description, Account = accountRepository.GetByUsername(CurrentUserName), CommitteeId = lecturer.CommitteeId, Profile = new LecturerProfile() { BirthDate = birthDate, Degree = lecturer.Degree, Email = lecturer.Email, Post = lecturer.Post, Room = lecturer.Room, LecturerCategoryId = lecturer.LecturerCategoryId, RecordOfService = lecturer.RecordOfService }, Photo = photo }; lecturersRepository.Add(lecturerEntry); } TempData[Const.ActionResultInfo] = "Профиль преподавателя создан успешно"; return RedirectToAction("Lecturers"); } TempData[Const.ActionErrorInfo] = "Невозможно сохранить профиль преподавателя. Проверьте правильность введенных данных."; return View(); }
/// <summary> /// Create a new File object. /// </summary> /// <param name="id">Initial value of the Id property.</param> /// <param name="fileName">Initial value of the FileName property.</param> /// <param name="filePurposeId">Initial value of the FilePurposeId property.</param> public static File CreateFile(global::System.Int32 id, global::System.String fileName, global::System.Int32 filePurposeId) { File file = new File(); file.Id = id; file.FileName = fileName; file.FilePurposeId = filePurposeId; return file; }
/// <summary> /// Deprecated Method for adding a new object to the FileSet EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToFileSet(File file) { base.AddObject("FileSet", file); }