public ActionResult Create(ProductTextBlockFile model, HttpPostedFileBase image, HttpPostedFileBase filePdf) { try { model.Id = 0; var product = _context.ProductTextBlocks.First(a => a.Id == model.ProductTextBlockId); var categoryId = product.Product.CategoryId; model.Title = model.Title; var cache = new ProductTextBlockFile { ProductTextBlock = product, Title = model.Title, }; var file = image; if (file != null && !string.IsNullOrEmpty(file.FileName)) { string fileName = IOHelper.GetUniqueFileName("~/Content/Images", file.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); GraphicsHelper.SaveOriginalImage(filePath, fileName, file, 100); //file.SaveAs(filePath); cache.ImageSource = fileName; } file = filePdf; if (file != null && !string.IsNullOrEmpty(file.FileName)) { string fileName = IOHelper.GetUniqueFileName("~/Content/Files", file.FileName); string filePath = Server.MapPath("~/Content/Files"); filePath = Path.Combine(filePath, fileName); file.SaveAs(filePath); cache.FileName = fileName; } product.ProductTextBlockFiles.Add(cache); _context.SaveChanges(); return RedirectToAction("Details", "Category", new { id = categoryId }); } catch (Exception ex) { TempData["errorMessage"] = ex.Message; return View(model); } }
public ActionResult Edit(ProductTextBlockFile model, HttpPostedFileBase image, HttpPostedFileBase filePdf) { try { var product = _context.ProductTextBlockFiles.First(a => a.Id == model.Id); var categoryId = product.ProductTextBlock.Product.CategoryId; TryUpdateModel(product, new[] {"Title"}); var file = image; if (file != null && !string.IsNullOrEmpty(file.FileName)) { ImageHelper.DeleteImage(product.ImageSource); string fileName = IOHelper.GetUniqueFileName("~/Content/Images", file.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); GraphicsHelper.SaveOriginalImage(filePath, fileName, file, 100); //file.SaveAs(filePath); product.ImageSource = fileName; } file = filePdf; if (file != null && !string.IsNullOrEmpty(file.FileName)) { ImageHelper.DeleteFile(product.FileName); string fileName = IOHelper.GetUniqueFileName("~/Content/Files", file.FileName); string filePath = Server.MapPath("~/Content/Files"); filePath = Path.Combine(filePath, fileName); file.SaveAs(filePath); product.FileName = fileName; } _context.SaveChanges(); return RedirectToAction("Details", "Category", new { id = categoryId }); } catch (Exception ex) { TempData["errorMessage"] = ex.Message; return View(model); } }