public async Task<IHttpActionResult> PutCellPhone(int id, CellPhone cellPhone) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != cellPhone.Id) { return BadRequest(); } db.Entry(cellPhone).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CellPhoneExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> PostCellPhone(CellPhone cellPhone) { if (!Request.Content.IsMimeMultipartContent()) { return BadRequest(); } if (HttpContext.Current.Request.Files.Count > 0 && HttpContext.Current.Request.Files[0].ContentLength > 0) { HttpPostedFile fileData = HttpContext.Current.Request.Files[0]; string extention = Path.GetExtension(fileData.FileName); if (string.Equals(".xls", extention) || string.Equals(".xlsx", extention)) { string temp_path = HttpContext.Current.Server.MapPath( string.Format("~/Images/{0}{1}", Guid.NewGuid(), extention)); fileData.SaveAs(temp_path); cellPhone.ImageUrl = temp_path; } } if (!ModelState.IsValid) { return BadRequest(ModelState); } db.CellPhones.Add(cellPhone); await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new { id = cellPhone.Id }, cellPhone); }