2

I have small data and big XML (1.5MB), which I'm trying to post to Web.Api post method, later will be saved in the database. I'm trying to post it as Multipart data content but don't know how to read. What I'm doing wrong?

Example sending data:

using (var httpClient = new HttpClient())
{

    string bigXml = File.ReadAllText("XML/Big.xml");

    var url = "http://localhost:16065/api/Home/Post";

    httpClient.DefaultRequestHeaders.Accept.Clear();
    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var values = new Dictionary<string, string>{
        {"Alpha", "Alpha value"},
        {"Beta", "Beta value"},
        //{"XMLfile", bigXml},  // Invalid URI: The Uri string is too long.
    };

    HttpContent httpContent = new FormUrlEncodedContent(values);
    StringContent stringContent = new StringContent(bigXml, System.Text.Encoding.UTF32);

    MultipartContent multipartContent = new MultipartContent();
    multipartContent.Add(stringContent);
    multipartContent.Add(httpContent);

    HttpResponseMessage response = httpClient.PostAsync(url, multipartContent).Result;

    response.EnsureSuccessStatusCode();
    var result = response.Content.ReadAsStringAsync().Result;
    Console.WriteLine(result);

}

Receiving data in Web.Api method:

public string Post()
{
    if (!Request.Content.IsMimeMultipartContent())
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);

    HttpRequest request = HttpContext.Current.Request;

    string root = HttpContext.Current.Server.MapPath("~/App_Data");
    var provider = new MultipartFormDataStreamProvider(root);
    MultipartFormDataContent mpfdc = new MultipartFormDataContent();

    try
    {
        Request.Content.ReadAsMultipartAsync(provider);

        string formXML = provider.FormData["FormXML"];
        string content = provider.FormData["content"];

        var response = Request.CreateResponse();
        response.Content = mpfdc;
        response.StatusCode = HttpStatusCode.OK;
        return "1";
    }
    catch (System.Exception e)
    {
        return "2";
    }
}
serge klokov
  • 81
  • 1
  • 11
  • What's the exception? – Jacob Roberts Jul 24 '15 at 19:15
  • no exception, but formXML and content are null – serge klokov Jul 24 '15 at 19:18
  • Looks like your `ReadAsMultipartAsync` isn't completed before it tries to read it. `ReadAsMultipartAsync` is a task so it is running on another thread and since you are not awating it, it is off doing it thing as the code continues on the current thread. Try `ReadAsMultipartAsync(provider).Wait();` or mark your method as async and do `await ReadAsMultipartAsync(provider);` – Jacob Roberts Jul 24 '15 at 20:58
  • Check out this question and answer : http://stackoverflow.com/questions/15201255/request-content-readasmultipartasync-never-returns – granadaCoder Jan 25 '16 at 19:14
  • My "struggle" may help as well : http://stackoverflow.com/questions/34999033/parsing-consuming-a-multipartformdatacontent-set-by-mvc-on-the-webapi-side/ – granadaCoder Jan 26 '16 at 15:44

0 Answers0