Error: The request entity's media type 'multipart / form-data' is not supported for this resource

0

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?

    
asked by anonymous 04.01.2017 / 03:50

1 answer

2

To receive a file via WebAPI, and reading the data is a bit different from just sending POST in JSON format.

When performing a POST with type multipart , it should be read with a MultipartFormDataStreamProvider .

Here's how to do this:

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 on submitting files in Multipart MIME format .

    
04.01.2017 / 09:07