public ActionResult Create([Bind(Include = "Title,FileSetType,urls,Description,StartDate,EndDate,SklepId")] Ulotka ulotka) { ulotka.ApplicationUserId = User.Identity.GetUserId(); ulotka.PublishDate = DateTime.Now; //ulotka.FileSetType ModelState.Clear(); var urls = ulotka.urls.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (TryValidateModel(ulotka) && ServerTools.TempFolderContains(urls)) { try { if (ulotka.FileSetType == FileSetType.Pdf) { string filePath = Path.Combine(ServerTools.TempFolderPath, urls.First()); ulotka.urls = ServerTools.ConvertPdfToImages(filePath); System.IO.File.Delete(filePath); } else { string destinationUrlList = string.Empty; foreach (var item in urls) { //todo better naming /sklep/ulotka123 string sourcePath = Path.Combine(ServerTools.TempFolderPath, item); string folder = ulotka.FileSetType == FileSetType.Pdf ? "Data/Pdf" : "Data/Images"; string destinationFilePath = Path.Combine(ServerTools.MediaFolderPath(folder), item); System.IO.File.Move(sourcePath, destinationFilePath); destinationUrlList += ServerTools.RelativePath(destinationFilePath) + ";"; } ulotka.urls = destinationUrlList; } db.Ulotki.Add(ulotka); db.SaveChanges(); return(RedirectToAction("Index")); } catch (Exception) { string key = "urls"; if (ModelState.ContainsKey(key)) { ModelState.Remove(key); } ModelState.AddModelError(key, "Wybrane pliki nie są wspierane."); } } ViewBag.SklepId = new SelectList(db.Sklepy, "Id", "Id", ulotka.SklepId); return(View(ulotka)); }
public ActionResult Create([Bind(Include = "Id,LogoUrl,KategoriaId")] Sklep sklep) { if (ModelState.IsValid && ServerTools.TempFolderContains(sklep.LogoUrl)) { string sourcePath = Path.Combine(ServerTools.TempFolderPath, sklep.LogoUrl); string destinationPath = Path.Combine(ServerTools.MediaFolderPath("Logos"), sklep.LogoUrl); System.IO.File.Move(sourcePath, destinationPath); //todo server side image resize sklep.LogoUrl = ServerTools.RelativePath(destinationPath); db.Sklepy.Add(sklep); db.SaveChanges(); return(RedirectToAction("Index")); } //todo chech if logo uploaded ViewBag.KategoriaId = new SelectList(db.Kategorie, "Id", "Id"); return(View(sklep)); }