string imagePath = @"D:\640.png";
string apiUrl = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=" + Token;
using (var client = new HttpClient())
using (var content = new MultipartFormDataContent())
{
// 读取图片文件的字节数组
byte[] imageBytes = File.ReadAllBytes(imagePath);
// 将图片数据添加到表单数据中
var imageContent = new ByteArrayContent(imageBytes);
imageContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/png");
content.Add(imageContent, "media", "640.png");
var response = client.PostAsync(apiUrl, content).Result;
if (response.IsSuccessStatusCode)
{
// 读取响应内容
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
//结果会来到这里:responseBody:{\"errcode\":41005,\"errmsg\":\"media data missing hint: [Rnqx2a002357-2] rid: 68b8078f-0b18b774-77c5ca41\"}
return "";
}
else
{
return "";
}
}
感谢

以前都没写过上传图片吗 using System; using System.IO; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; class Program { static async Task Main() { string filePath = "D:\\test.jpg"; string uploadUrl = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=xxxx&type=image"; using (var httpClient = new HttpClient()) { using (var content = new MultipartFormDataContent()) { content.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data"); var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(filePath)); // 关键代码 "\"" content.Add(fileContent, "media", "\"" + Path.GetFileName(filePath) + "\""); var response = await httpClient.PostAsync(uploadUrl, content); var resultStr = await response.Content.ReadAsStringAsync(); Console.WriteLine(resultStr); } } } }