public async Task <HttpResponseMessage> PostFormData() { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } VideoUnitDTO video = new VideoUnitDTO { DateLoaded = DateTime.Now, GeneratedName = Math.Abs(Guid.NewGuid().GetHashCode()).ToString(), Name = "DefaultName", UserName = User.Identity.Name }; string root = HttpContext.Current.Server.MapPath("~/Videos"); var path = "/" + User.Identity.Name + "/" + video.GeneratedName + "/"; DirectoryInfo directory = Directory.CreateDirectory(root + path); var fileName = "Videos" + path; var provider = new ImpulseFormDataStreamProvider(directory.FullName); try { await Request.Content.ReadAsMultipartAsync(provider); foreach (var file in provider.FileData) { if (file.Headers.ContentType.MediaType.Contains("video")) { video.FullPath = "/" + fileName + Path.GetFileName(file.LocalFileName); video.MimeType = file.Headers.ContentType.MediaType; } else { var exceptionMessage = new HttpResponseMessage(HttpStatusCode.UnsupportedMediaType) { ReasonPhrase = "Файл не является видео", Content = new StringContent("Тип загруженного объекта " + file.Headers.ContentType.MediaType) }; throw new HttpResponseException(exceptionMessage); } } return(Request.CreateResponse(HttpStatusCode.OK, video)); } catch (HttpResponseException e) { return(Request.CreateErrorResponse(e.Response.StatusCode, e.Response.ReasonPhrase)); } }
public async Task <HttpResponseMessage> SaveImage(VideoUnitDTO video) { byte[] image = Convert.FromBase64String(video.Image); string path = "/Videos/" + User.Identity.Name + "/" + video.GeneratedName + "/"; string fullPath = HttpContext.Current.Server.MapPath("~" + path); if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } File.WriteAllBytes(fullPath + "/img.png", image); video.Image = path + "img.png"; VideoUnit vid = Mapper.Map <VideoUnitDTO, VideoUnit>(video); string id = await service.SaveVideoAsync(vid); return(Request.CreateResponse(HttpStatusCode.OK, id)); }