C# 上传文件添加附加参数

using (var client = new HttpClient())
{
    using (var multipartFormDataContent = new MultipartFormDataContent())
    {
        var values = new[]
        {
            new KeyValuePair("c", "3"),
            new KeyValuePair("c", "2"),
            new KeyValuePair("d", "2")
             //other values
        };

        foreach (var keyValuePair in values)
        {
            multipartFormDataContent.Add(new StringContent(keyValuePair.Value),
                String.Format("\"{0}\"", keyValuePair.Key));
        }

        multipartFormDataContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(@"D:\test.jpg")),
            "\"pic\"",
            "\"test.jpg\"");

        var requestUri = "http://localhost:8080";
        var html = client.PostAsync(requestUri, multipartFormDataContent).Result.Content.ReadAsStringAsync().Result;
    }
}

 

 
posted @ 2021-07-01 13:53  卖雨伞的小男孩  阅读(305)  评论(0编辑  收藏  举报