0
I’m trying to make 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 em System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken) em System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)
I am trying to upload a text file (.txt)
Follow the code of 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 view upload
<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?
I edited my question. But I haven’t been able to perform the Post yet. Now it’s returning me the following message: A possibly dangerous Request.Path value has been detected in the client (>).
– Renan Carlos