none
C# 中 如何使用 httpclient 以form-data形式上传文件到服务器 RRS feed

  • 问题

  • using (var multipartFormDataContent = new MultipartFormDataContent())
                {
                    foreach (var keyValuePair in paramList)
                    {
                        httpClient.DefaultRequestHeaders.Add(keyValuePair.Key, keyValuePair.Value);
                    }
                    multipartFormDataContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
                    if (!File.Exists(filePath))
                        return null;
                    string fileName = Path.GetFileName(filePath);
                    multipartFormDataContent.Add(new StreamContent(new MemoryStream(File.ReadAllBytes(@filePath))), "file","file");
                    HttpResponseMessage response = await httpClient.PostAsync(url, multipartFormDataContent);
                    return response;
                }
    这是我写的代码,我如果在工程里提交的话 会返回说键或者值没加进去,请问我哪里出问题
    2019年8月15日 9:51

答案

  • Hi,

    你可以参考一下下面这些文章。

    C# HttpClient Post 参数同时上传文件

    C# HttpClient上传文件到服务器(multipart/form-data)

    Regards,

    Kyle

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 michealCui 2019年8月19日 6:59
    2019年8月16日 2:56
    版主