不知道是不是本地的原因,没有在服务器上面运行。但是上传临时素材图片总是出错:{"errcode":41005,"errmsg":"media data missing hint: [RqTrBA0887e384] rid: 678013a7-0feaa66c-4ab9d3ad"}
代码如下:
// <summary>
/// 上传公众号临时素材
/// </summary>
/// <param name="filePath">本地文件路径</param>
/// <returns>MediaId</returns>
public static async Task<string> UploadTempMediaAsync(string type, string filePath)
{
string url = $"https://api.weixin.qq.com/cgi-bin/media/upload?access_token={WeChatConfig.AccessToken}&type={type}";
using (var client = new HttpClient())
{
// 读取文件内容
byte[] fileBytes = File.ReadAllBytes(filePath);
string fileName = Path.GetFileName(filePath);
// 打印文件大小(调试用)
Console.WriteLine($"File size: {fileBytes.Length} bytes");
// 创建MultipartFormDataContent
using (var content = new MultipartFormDataContent())
{
ByteArrayContent fileContent = new(fileBytes);
fileContent.Headers.ContentType=new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
content.Add(fileContent, "media", fileName);
//发送POST请求
HttpResponseMessage response = await client.PostAsync(url, content);
//检查响应状态
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
var wuts = JsonSerializer.Deserialize<WeChatUploadTempResponse>(responseContent);
return wuts?.MediaId!;
}
return null!;
}
}
}
你好,测试号可以上传临时素材,报错41005有中文乱码