public async Task<IHttpActionResult> Post() { var image = new UploadImage(); try { var provider = new MultipartMemoryStreamProvider(); await Request.Content.ReadAsMultipartAsync(provider); foreach (var file in provider.Contents) { // Seems kind of hackish as a method for determining where the file name is in the content stream if (file.Headers.ContentDisposition.FileName != null) { // Get file specifics and save image.fileName = file.Headers.ContentDisposition.FileName.Trim('\"'); image.file = await file.ReadAsByteArrayAsync(); image = await this.processor.SaveAndResizeImage(image); } } } catch (Exception exception) { return InternalServerError(exception); } // Technically we could return the upload image json if we needed that client side return Ok(image.file); }
/// <summary> /// Save an Image and resize if necessary /// </summary> /// <param name="uploadImage"></param> /// <returns></returns> public async Task<UploadImage> SaveAndResizeImage(UploadImage uploadImage) { try { var uploadPath = Path.Combine(this.filePath, Path.GetFileName(uploadImage.fileName)); using (var fs = new FileStream(uploadPath, FileMode.Create)) { await fs.WriteAsync(uploadImage.file, 0, uploadImage.file.Length); var resizeImage = imageHelper.GetImage(uploadImage.file); if (imageHelper.ResizeNeeded(resizeImage, maxWidth, maxHeight)) { var resized = imageHelper.ResizeImage(resizeImage, maxWidth, maxHeight); using (var resizedFs = new FileStream(Path.Combine(this.filePath, Path.GetFileName(GetResizedFileName(uploadImage.fileName))), FileMode.Create)) { await resizedFs.WriteAsync(resized, 0, resized.Length); // Set the original image byte stream to the new resized value uploadImage.file = resized; } } } } catch (Exception exception) { throw new Exception("Error saving file: " + exception.Message); } return uploadImage; }
public async Task SaveImage() { CreateImage("testImage.png", 500, 500, ImageFormat.Png); var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationManager.AppSettings["filePath"], Path.GetFileName("testImage.png")); var uploadImage = new UploadImage() { file = File.ReadAllBytes(filepath), fileName = "testImage2.png" }; var processor = new ImageProcessor(new ImageHelper()); var image = await processor.SaveAndResizeImage(uploadImage); Assert.AreEqual(image, uploadImage); Assert.IsTrue(FileExists(image.fileName)); DeleteImage("testImage.png"); DeleteImage("testImage2.png"); }
public async Task Post() { // Mock dependency var processor = new Mock<IImageProcessor>(); var file = new UTF8Encoding(true).GetBytes("This is some text in the file blah."); var uploadImage = new UploadImage() { filepath = "a/filepath", id = 1, thumbnailPath = "a/filepath/thumbnails", file = file, fileName = "testImage" }; processor.Setup(x => x.SaveAndResizeImage(It.IsAny<UploadImage>())).ReturnsAsync(uploadImage); // Setup content headers for POST var content = GetContentHeader(uploadImage); // Setup controller var controller = new ImagesController(processor.Object) { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; controller.Request.Content = content; // Post file var result = controller.Post(); var contentResult = await result as OkNegotiatedContentResult<byte[]>; Assert.AreEqual(uploadImage.file, contentResult.Content); }
private MultipartFormDataContent GetContentHeader(UploadImage uploadImage) { var fileContent = new ByteArrayContent(uploadImage.file); fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { Name = "attachment", FileName = uploadImage.fileName }; return new MultipartFormDataContent { fileContent }; }