13
public class Sampleontroller:apicontroller
 {    
    public void PostBodyMethod() {
        HttpRequestMessage request=this.request;
     //How to read the multi part data in the method
    }
}

I am sending a multi part data to webapi controller. How to read the contents in the method?

Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
user1599992
  • 157
  • 1
  • 3
  • 10

2 Answers2

8

An 'async' example:

public async Task<HttpResponseMessage> PostSurveys()
    {
        // Verify that this is an HTML Form file upload request
        if (!Request.Content.IsMimeMultipartContent("form-data"))
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }

            //Destination folder
            string uploadFolder = "mydestinationfolder";

            // Create a stream provider for setting up output streams that saves the output under -uploadFolder-
            // If you want full control over how the stream is saved then derive from MultipartFormDataStreamProvider and override what you need.            
            MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(uploadFolder );                
            MultipartFileStreamProvider multipartFileStreamProvider = await Request.Content.ReadAsMultipartAsync(streamProvider);

            // Get the file names.
            foreach (MultipartFileData file in streamProvider.FileData)
            {
                //Do something awesome with the files..
            }
}
D.Rosado
  • 5,634
  • 3
  • 36
  • 56
3

Have a look at the article by Mike Wasson: http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2

Or if you are doing file uploads, here: www.strathweb.com/2012/08/a-guide-to-asynchronous-file-uploads-in-asp-net-web-api-rtm/

Youngjae
  • 24,352
  • 18
  • 113
  • 198
Filip W
  • 27,097
  • 6
  • 95
  • 82