public ActionResult Create(Product prod, HttpPostedFileBase postedFile) { try { if (postedFile != null) { string path = Server.MapPath("/Content/stickerspic/"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } postedFile.SaveAs(path + Path.GetFileName(postedFile.FileName)); ViewBag.Message = "Picture uploaded successfully."; prod.imgprod = Path.GetFileName(postedFile.FileName); // just call the service and it will do the work check service production for more informations prod.imgprod = prod.imgprod; sp.add_product(prod); return(RedirectToAction("IndexProducts")); } sp.add_product(prod); return(RedirectToAction("IndexProducts")); } catch (NullReferenceException e) { return(RedirectToAction("IndexProducts")); } }
public ActionResult Create(Product prod, HttpPostedFileBase item) { try { if (ModelState.IsValid && verifyFiles(item))// check if the model state is valid , and the file (image in the input is valid) { string name = "name" + prod.nameprod + "im" + DateTime.Now.Minute + DateTime.Now.Millisecond + Path.GetExtension(item.FileName); //creating the name of the product var path = Path.Combine(Server.MapPath("../Content/stickerspic/"), name); //creating the path of the image combining the name of the product with the minute and millisecond to get a unique name for it item.SaveAs(path); //image saved in the path prod.imgprod = path; sp.add_product(prod); } return(RedirectToAction("IndexProducts")); } catch { return(View()); } }