Skip to content

Instantly share code, notes, and snippets.

@victormejia
Last active January 4, 2016 06:49
Show Gist options
  • Save victormejia/8584215 to your computer and use it in GitHub Desktop.
Save victormejia/8584215 to your computer and use it in GitHub Desktop.
File POST in Web API
[HttpPost, Route("api/upload")]
public async Task<HttpResponseMessage> Upload()
{
if (!Request.Content.IsMimeMultipartContent())
return Request.CreateErrorResponse(HttpStatusCode.UnsupportedMediaType, "expected multi-part form content.");
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var file in provider.Contents)
{
var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
var buffer = await file.ReadAsByteArrayAsync();
//Do whatever you want with filename and its binaray data.
}
return Ok();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment