I'm trying to do a POST in a WebApi that is returning me the following error:
The request entity's media type 'multipart / form-data' is not supported for this resource.
ExceptionMessage: No MediaTypeFormatter is available to read an object of type 'File' from content with media type 'multipart / form-data'.
ExceptionType: System.Net.Http.UnsupportedMediaTypeException in System.Net.Http.HttpContentExtensions.ReadAsAsync [T] (HttpContent content, Type type, IEnumerable'1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken) at the System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync (HttpRequestMessage request, Type type, IEnumerable'1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)
I'm trying to upload a text file (.txt)
Here is the code for my controller:
public async Task<HttpResponseMessage> PostUpload()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/Uploads/Texto");
var provider = new MultipartFormDataStreamProvider(root);
try
{
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider);
// This illustrates how to get the file names.
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
and my upload view
<form action="http://localhost:61877/api/Arquivos" enctype="multipart/form-data" method="POST">
<label for="relatedFile">File:</label>
<input name="relatedFile" size="40" type="file">
<input type="submit">
</form>
What am I doing wrong?