public async Task<List<PhotoDto>> Upload(System.Net.Http.HttpRequestMessage Request) { var photos = new List<PhotoDto>(); if (!Request.Content.IsMimeMultipartContent("form-data")) { throw new HttpResponseException(HttpStatusCode.BadRequest); } var provider = await Request.Content.ReadAsMultipartAsync<InMemoryMultipartFormDataStreamProvider>(new InMemoryMultipartFormDataStreamProvider()); NameValueCollection formData = provider.FormData; IList<HttpContent> files = provider.Files; foreach (var file in files) { var filename = new FileInfo(file.Headers.ContentDisposition.FileName.Trim(new char[] { '"' }) .Replace("&", "and")).Name; Stream stream = await file.ReadAsStreamAsync(); var bytes = StreamHelper.ReadToEnd(stream); var photo = new Photo(); if (repository.GetAll().Count(x => x.Filename == filename) < 1) { repository.Add(photo); } else { photo = repository.GetAll().Single(x => x.Filename == filename); } photo.Filename = filename; photo.Bytes = bytes; photo.ContentType = Convert.ToString(file.Headers.ContentType); photos.Add(new PhotoDto(photo)); } this.uow.SaveChanges(); return photos; }
public PhotoDto(Photo photo) { this.Src = string.Format(@"/api/photo/serve?guid={0}", photo.Guid); this.Base64String = Convert.ToBase64String(photo.Bytes); }