Last active
July 13, 2017 06:18
-
-
Save jpvelasco/a6a70e5c17fe1a90f5a68b576740660b to your computer and use it in GitHub Desktop.
Image upload web API endpoint controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> Uploads an image file </summary> | |
/// <returns>IHttpActionResult.</returns> | |
[Route("api/file")] | |
[HttpPost] | |
public IHttpActionResult UploadImage() | |
{ | |
if (Request.Content.IsMimeMultipartContent() == false) | |
{ | |
throw new UnsupportedMediaTypeException("unsupported media type", null); | |
} | |
IEnumerable<HttpContent> httpContentParts = null; | |
Task.Factory.StartNew(() => httpContentParts = Request.Content.ReadAsMultipartAsync() | |
.Result.Contents, | |
CancellationToken.None, | |
TaskCreationOptions.LongRunning, | |
TaskScheduler.Default).Wait(); | |
HttpContent fileContents = httpContentParts.First(f => f.Headers.ContentType != null && | |
// NOTE: file contents can have multiple filters, here the ContentDisposition Name is set by | |
// the client, thus we match 'file_data'. This is simply to be used as an example, your filter options | |
// may vary depending on your requirements. | |
f.Headers.ContentDisposition.Name.Equals("\"file_data\"", StringComparison.InvariantCultureIgnoreCase)); | |
UploadFileContentsToBlobStorage(fileContents); | |
return Ok(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment