public async Task<IHttpActionResult> EditCategory() { //if (!ModelState.IsValid) //{ // return BadRequest(ModelState); //} //if (id != category.CategoryId) //{ // return BadRequest(); //} //db.Entry(category).State = EntityState.Modified; //try //{ // await db.SaveChangesAsync(); //} //catch (DbUpdateConcurrencyException) //{ // if (!CategoryExists(id)) // { // return NotFound(); // } // else // { // throw; // } //} //return StatusCode(HttpStatusCode.NoContent); // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = HttpContext.Current.Server.MapPath("~/Resources/img"); var provider = new CustomMultipartFormDataStreamProvider(root); try { //read content into a buffer Request.Content.LoadIntoBufferAsync().Wait(); // Read the form data. await Request.Content.ReadAsMultipartAsync(provider); var categoryName = Extraction.ExtractValue("cat-name", provider.FormData); var id = Guid.Parse(Extraction.ExtractValue("cat-id", provider.FormData)); if (CategoryExists(id)) { var category = await db.Categories.FindAsync(id); category.Name = categoryName; if (provider.FileData.Any()) { var fileName = FileNameHelper.GetFileNameFromLocalPath(provider.FileData[0].LocalFileName); var path = WebConfigurationManager.AppSettings["ImageUrlPath"]; category.ImageUrl = String.Format("{0}/{1}", path, fileName); } db.Entry(category).State = EntityState.Modified; await db.SaveChangesAsync(); return Ok(); } return BadRequest(); } catch (Exception e) { return InternalServerError(e); } }
public async Task<IHttpActionResult> CreateCategory() { // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = HttpContext.Current.Server.MapPath("~/Resources/img"); var provider = new CustomMultipartFormDataStreamProvider(root); try { //read content into a buffer Request.Content.LoadIntoBufferAsync().Wait(); // Read the form data. await Request.Content.ReadAsMultipartAsync(provider); var categoryName = Extraction.ExtractValue("cat-name", provider.FormData); var fileName = FileNameHelper.GetFileNameFromLocalPath(provider.FileData[0].LocalFileName); var path = WebConfigurationManager.AppSettings["ImageUrlPath"]; Category category = new Category { Name = categoryName, ImageUrl = String.Format("{0}/{1}", path, fileName) }; db.Categories.Add(category); await db.SaveChangesAsync(); return Ok(); } catch (System.Exception e) { return InternalServerError(e); } }