public HttpResponseMessage SaveOrganisationalUnitImage(HttpRequestMessage request, int categoryId, string organisationalUnitId) //, HttpPostedFileBase imageFile) { OrganisationalUnitInfo ou = _service.GetOrganisationalUnitInfo(categoryId, organisationalUnitId); if (ou != null) { var file = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null; if (file != null && file.ContentLength > 0) { var path = ""; //delete any old image file if (!String.IsNullOrWhiteSpace(ou.ImagePath)) { path = Path.Combine(_operatorImagesFolder, ou.ImagePath); if (File.Exists(path)) { try { File.Delete(path); } catch { } } } //generate new filename string fileExtension = Path.GetExtension(file.FileName); string filename = ""; while (path == "" || System.IO.File.Exists(path)) { filename = Guid.NewGuid() + fileExtension; path = Path.Combine(_operatorImagesFolder, filename); } //save the new image file try { file.SaveAs(path); //update entity ou.ImagePath = filename; _service.UpdateOrganisationalUnitInfo(ou); ViewModels.Shared.OrganisationalUnitInfoViewModel model = new ViewModels.Shared.OrganisationalUnitInfoViewModel(ou); return(request.CreateResponse <ViewModels.Shared.OrganisationalUnitInfoViewModel>(HttpStatusCode.OK, model)); } catch { } ModelState.AddModelError(null, "Kunde inte spara bilden."); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } } return(new HttpResponseMessage(HttpStatusCode.BadRequest)); }
[ValidateModel] //this will handle validation (and return with any errors) before method is run public HttpResponseMessage SaveOrganisationalUnit(HttpRequestMessage request, int categoryId, string organisationalUnitId, [FromBody] ViewModels.Shared.OrganisationalUnitInfoViewModel organisationalUnit) { OrganisationalUnitInfo ou = _service.GetOrganisationalUnitInfo(categoryId, organisationalUnitId); if (ou != null) { //transfer info from view model to existing entity ou = organisationalUnit.ToEntity(ou); _service.UpdateOrganisationalUnitInfo(ou); return(new HttpResponseMessage(HttpStatusCode.OK)); } return(new HttpResponseMessage(HttpStatusCode.BadRequest)); }
public HttpResponseMessage SaveOrganisationalUnitImage(HttpRequestMessage request, int categoryId, string organisationalUnitId) //, HttpPostedFileBase imageFile) { OrganisationalUnitInfo ou = _service.GetOrganisationalUnitInfo(categoryId, organisationalUnitId); if (ou != null) { var file = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null; if (file != null && file.ContentLength > 0) { var path = ""; //delete any old image file if (!String.IsNullOrWhiteSpace(ou.ImagePath)) { path = Path.Combine(_operatorImagesFolder, ou.ImagePath); if (File.Exists(path)) { try { File.Delete(path); } catch { } } } //generate new filename string fileExtension = Path.GetExtension(file.FileName); string filename = ""; while (path == "" || System.IO.File.Exists(path)) { filename = Guid.NewGuid() + fileExtension; path = Path.Combine(_operatorImagesFolder, filename); } //save the new image file try { file.SaveAs(path); //update entity ou.ImagePath = filename; _service.UpdateOrganisationalUnitInfo(ou); ViewModels.Shared.OrganisationalUnitInfoViewModel model = new ViewModels.Shared.OrganisationalUnitInfoViewModel(ou); return request.CreateResponse<ViewModels.Shared.OrganisationalUnitInfoViewModel>(HttpStatusCode.OK, model); } catch { } ModelState.AddModelError(null, "Kunde inte spara bilden."); return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } } return new HttpResponseMessage(HttpStatusCode.BadRequest); }