/// <summary>
/// 上传微信素材的两种方式
/// </summary>
/// <returns></returns>
public async Task<IActionResult> UploadLoadMedia()
{
string token = await GetTokenAsync();
#region 方式一:通过webClient
WebClient webClient = new WebClient();
var media = webClient.UploadFile(string.Format(WxApis.UploadMedia, token, "image"), $"{env.WebRootPath}/images/02.jpg");
var str = Encoding.Default.GetString(media);
#endregion
#region 方式二:通过HttpClient
HttpClient httpClient = new HttpClient();
using FileStream fileStream = new FileStream($"{env.WebRootPath}/images/02.jpg", FileMode.Open, FileAccess.Read);
var boundary = DateTime.Now.Ticks.ToString("X");
using (var content = new MultipartFormDataContent(boundary))
{
content.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");
content.Add(new StreamContent(fileStream), "buffer", "\"" + $"{env.WebRootPath}/images/02.jpg" + "\"");
content.Headers.TryAddWithoutValidation("Content-Disposition", $"form-data; name=\"media\";filename=\"02.jpg\"");
try
{
var json = await httpClient.PostAsync(string.Format(WxApis.UploadMedia, token, "image"), content).Result.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
//ex.Message;
}
}
#endregion
return Ok();
}