Error: The request Entity’s media type 'Multipart/form-data' is not supported for this Resource

Asked

Viewed 1,894 times

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?

1 answer

2


To receive file via Webapi, and reading the data is slightly different than just emviar POST in JSON format.

When a POST with type is made multipart, reading shall be done with a MultipartFormDataStreamProvider.

Here’s an example of how to do this reading:

public Task<HttpResponseMessage> PostFile() 
{ 
    HttpRequestMessage request = this.Request; 
    if (!request.Content.IsMimeMultipartContent()) 
    { 
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); 
    } 

    string root = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/uploads"); 
    var provider = new MultipartFormDataStreamProvider(root); 

    var task = request.Content.ReadAsMultipartAsync(provider). 
        ContinueWith<HttpResponseMessage>(o => 
    { 

        string file1 = provider.BodyPartFileNames.First().Value;
        // this is the file name on the server where the file was saved 

        return new HttpResponseMessage() 
        { 
            Content = new StringContent("File uploaded.") 
        }; 
    } 
    ); 
    return task; 
} 

You can read more about this in this article about sending files in Multipart MIME format.

  • 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 (>).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.