public void AddFile(FileModel file) { var f = from ff in context.FileModels where ff.ProductID == file.ProductID select ff; if (f.Count() > 0) { // ograniczenie do tylko jednego pliku var ph = context.FileModels.Single(x => x.ProductID == file.ProductID); ph.Name = file.Name; ph.ContentType = file.ContentType; ph.ProductID = file.ProductID; ph.Data = file.Data; } else { FileModel fm = new FileModel(); fm.Name = file.Name; fm.ContentType = file.ContentType; fm.ProductID = file.ProductID; fm.Data = file.Data; context.FileModels.InsertOnSubmit(fm); } context.SubmitChanges(); }
// // GET: /Product/SetImage/5 public ActionResult SetImage(int id) { if (files.ValidateProductID(id)) { FileModel file = new FileModel(); file.ProductID = id; return View(file); } return RedirectToAction("Index", "Category"); }
public ActionResult SetImage(int id, FileModel photo) { try { photo.ProductID = id; if (Request.IsAjaxRequest()) { photo.Data = Request.BinaryRead(Request.ContentLength); photo.Name = Request.Headers.Get("X-File-Name"); photo.ContentType = Request.ContentType; string error = photo.Validate; if (error == null) { files.AddFile(photo); return Json(new { success = true }); } return Json(new { errors = error }); } else { if (ModelState.IsValid) { photo.Name = photo.file.FileName; photo.ContentType = photo.file.ContentType; byte[] tmp = new byte[photo.file.ContentLength]; photo.file.InputStream.Read(tmp, 0, photo.file.ContentLength); photo.Data = tmp; files.AddFile(photo); return RedirectToAction("Details", "Product", new { id = id }); } return View(photo); } } catch (Exception e) { if (Request.IsAjaxRequest()) { return Json(new { success = false, error = e.Message }); } else { ViewData["ERROR"] = e.Message; return View(); } } }
private void detach_FileModels(FileModel entity) { this.SendPropertyChanging(); entity.ProductModel = null; }
partial void DeleteFileModel(FileModel instance);
partial void UpdateFileModel(FileModel instance);
partial void InsertFileModel(FileModel instance);