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

weixin_38088844 2019-08-15 09:51:32
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;
}
这是我写的代码,我如果在工程里提交的话 会返回说键或者值没加进去,请问我哪里出问题
...全文
1583 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38101849 2019-08-16
  • 打赏
  • 举报
回复
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.

433

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧