Reading multi-page data MVA4 WebApi

I am sending multipart form-data to my WebApi controller. The content that interests me includes both the image and a different value than the image. I cannot read the image without problems, but I cannot figure out how to read the username part (with andy as the value). This is not in the FormData property of the provider. How can i get it?

Multi-format form data

------------ei4KM7KM7ae0GI3Ef1gL6ei4ae0ae0
Content-Disposition: form-data; name="Filename"

image.jpg
------------ei4KM7KM7ae0GI3Ef1gL6ei4ae0ae0
Content-Disposition: form-data; name="username"

andy
------------ei4KM7KM7ae0GI3Ef1gL6ei4ae0ae0
Content-Disposition: form-data; name="Filedata"; filename="image.jpg"
Content-Type: application/octet-stream


------------ei4KM7KM7ae0GI3Ef1gL6ei4ae0ae0
Content-Disposition: form-data; name="Upload"

Submit Query
------------ei4KM7KM7ae0GI3Ef1gL6ei4ae0ae0--

Controller code

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

var task = request.Content.ReadAsMultipartAsync(provider).ContinueWith<HttpResponseMessage>(o =>
{ 
    ...
}
+5
source share
2 answers

There is a good article on

Your variable providermust have a NameValueCollection property called FormData. Therefore, you should do something like:

string username = provider.FormData.GetValues("username").SingleOrDefault();

NB. , FormData. RC : Microsoft.AspNet.WebApi.Client.4.0.20505.0 Nuget WebApi MVC 4 Microsoft.AspNet.WebApi.Client.4.0.20710.0.

+7

, , , -, :

string username = provider.Contents.First(x=>x.Headers.ContentDisposition.Name == "\"username\"").ReadAsStringAsync().Result;

?

+1

All Articles