Last active
January 4, 2016 06:49
-
-
Save victormejia/8584215 to your computer and use it in GitHub Desktop.
File POST in Web API
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
[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