Skip to content

Instantly share code, notes, and snippets.

@jpvelasco
Last active July 13, 2017 06:18
Show Gist options
  • Save jpvelasco/a6a70e5c17fe1a90f5a68b576740660b to your computer and use it in GitHub Desktop.
Save jpvelasco/a6a70e5c17fe1a90f5a68b576740660b to your computer and use it in GitHub Desktop.
Image upload web API endpoint controller
/// <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