Ultimately I want to have a simple file upload page using as basic a jQuery code as possible with the c# code on the server but I keep receiving a null IFormFile on the server.
There are lots of examples of how this can work with an Ajax POST with Form-data and receiving an IFormFile on an ASP.NET Web API controller action. However, it seems every one of them has had to try multiple tweaks to the code to get theirs working.
I have made a simple solution with just the API part and using Postman to send a file to it. I have tried different combinations of Content-Type and Accept headers but I always receive a null file.
To satisfy some hosting requirements, I am using OWIN to self-host the application. Here is the controller code, I have used the default values controller and edited the post-action...
public string Post([FromBody]IFormFile file)
{
if (file != null)
{
return "Success: We got something!!!";
}
else
{
return "ERROR: We got nothing :-(";
}
}
My StartUp.cs configuration is below. Although my example shows multipart/form-data, I have also tried application/JSON and other formats including leaving those blank.
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
config.Routes.MapHttpRoute("default", "{controller}");
config.EnableCors();
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("multipart/form-data"));
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
Then here are 2 screenshots of my latest postman attempt. I have hastily put them up on a Wix server so apologies if they are not yet visible
At this point, I'll be happy for the Post method to receive a not null value for a file.