public HttpResponseMessage Post() { HttpResponseMessage result = null; var httpRequest = HttpContext.Current.Request; if (httpRequest.Files.Count > 0) { var posters = new List<Poster>(); foreach (string file in httpRequest.Files) { var postedFile = httpRequest.Files[file]; var filePath = HttpContext.Current.Server.MapPath(POSTER_IMG_LOCATION_PROJECT + postedFile.FileName); postedFile.SaveAs(filePath); var fileLocation = POSTER_IMG_LOCATION_WEB + postedFile.FileName; var movieId = Guid.Parse(httpRequest.Form["movieId"]); var poster = new Poster { PosterId = Guid.NewGuid(), FileLocation = fileLocation, MovieId = movieId }; db.Posters.Add(poster); posters.Add(poster); } db.SaveChanges(); result = Request.CreateResponse(HttpStatusCode.Created, posters); } else { result = Request.CreateResponse(HttpStatusCode.BadRequest); } return result; }
private void GetPosterImage(Poster poster) { poster.PosterId = Guid.NewGuid(); var webClient = new WebClient(); if (!string.IsNullOrEmpty(poster.ImageUrl)) { webClient.DownloadFile(poster.ImageUrl, HttpContext.Current.Server.MapPath("~/" + POSTER_IMAGE_LOCATION) + poster.MovieId + ".jpg"); poster.FileLocation = "http://localhost:55400/" + POSTER_IMAGE_LOCATION + poster.MovieId + ".jpg"; } }