public IHttpActionResult PostActor() { HttpRequestMessage request = this.Request; if (!request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = System.Web.HttpContext.Current.Server.MapPath("~/Content/images/actors"); // Get the uploaded image from the Files collection var httpPostedFile = HttpContext.Current.Request.Files["image"]; var actor = JsonConvert.DeserializeObject <Actor>(HttpContext.Current.Request.Form[0]); Validate(actor); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (httpPostedFile != null) { // Validate the uploaded image(optional) var extension = new FileInfo(httpPostedFile.FileName).Extension; var fileName = Guid.NewGuid() + extension; // Get the complete file path var fileSavePath = Path.Combine(root, fileName); while (File.Exists(fileSavePath)) { fileName = Guid.NewGuid() + extension; fileSavePath = Path.Combine(root, fileName); } // Save the uploaded file to "UploadedFiles" folder httpPostedFile.SaveAs(fileSavePath); actor.Image = "http://localhost:50000/Content/images/actors/" + fileName; } _dALActor.AddActor(actor); return(CreatedAtRoute("DefaultApi", new { id = actor.Id }, actor)); }
private void AddActorExecute() { try { if (!string.IsNullOrEmpty(Actor.Name) || !string.IsNullOrEmpty(Actor.Surname)) { Actor newActor = new Actor { Name = Actor.Name, Surname = Actor.Surname, Deleted = false }; _dALHelper.AddActor(newActor); actor_window.Close(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }