Uploading model along with uploading files

4

I have a web API built with ASP.NET WebAPI, I have a model that references a URI of a file (for example representing a photo in a photo gallery, or a product in a catalog) and I need to send the data to the API. / p>

As for uploading the file I've already been able to find how this is done in WebAPI on the asp.net site itself, the problem is that the default binder model does not work when the form is sent with enctype=multipart/form-data .

What I mean is that I have in the controller a method with this signature:

public async Task<IHttpActionResult> Post([FromBody] Model model)

If I do not send with enctype=multipart/form-data the model binder works and I can retrieve the data from the model, but I do not get the file. If I put this, I get the file but the model binder throws an exception

  

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

     

In MediaTypeFormatter is available to read an object of type 'Model' from content with media type 'multipart / form-data'.

The only solution I thought at the time was basically to send only the model first in a request, then in a separate request send the file and associate the URI in the model.

Is there any way to do both at the same time? Or the most common is actually doing it in two steps, first sending the model and then the file?

    
asked by anonymous 07.02.2015 / 00:21

1 answer

0

You should use enctype multipar / form-data, but your action should not have a method, you will have to do the handling of the values in the hand, through a foreach loop in MultipartFormDataStreamProvider.FormData.AllKeys , in case you really need to map the form data for an object (model) you can do this using Reflection

See if this link helps you:

link

    
14.01.2017 / 21:58