Reputation: 37
I am trying to get a File that is being sent (from Postman) in the body of a POST request to my REST API that is self hosted using Owin. While debugging I can confirm that the correct endpoint it being reached but HttpContext.Current returns null always. Pretty much HttpContext.anything returns null. So I don't understand how I am supposed to receive a File via POST request and process it in my REST API while using Owin self hosting. I have tried Request.GetOwinContext() but that doesn't return anything useful.
How do I achieve this?
Upvotes: 0
Views: 447
Reputation: 13234
The Request
property of a controller can help access the file.
public class FileController : ApiController
{
[HttpPost]
public async Task Post()
{
if (Request.Content.IsMimeMultipartContent())
{
var multipartContent = await Request.Content.ReadAsMultipartAsync();
// "thefile" is the form field name
HttpContent httpContent = multipartContent.Contents
.Where(c => c.Headers.ContentDisposition.Name == "\"thefile\"")
.Single();
// httpContent contains the file, for example:
var fileContents = await httpContent.ReadAsStringAsync();
}
}
}
Upvotes: 1