如何在Web.API Post方法中获取MultipartFormDataContent?

时间:2015-07-24 18:45:49

标签: c#

我有小数据和大XML(1.5MB),我试图发布到Web.Api post方法,稍后将保存在数据库中。 我试图将其发布为Multipart数据内容,但不知道如何阅读。我做错了什么?

发送数据的示例:

66c65
<     if 'upscale' not in opts and x < width:
---
>     if 'upscale' not in opts and x < float(width or 0):

在Web.Api方法中接收数据:

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);

}

0 个答案:

没有答案