zoukankan      html  css  js  c++  java
  • 微信三方平台开发上传base64格式图片至临时素材

     1   public string UploadImgByB64(string b64)
     2         {
     3             //access_token 需要自己获取
     4             string access_token = getToken();
     5             string url = $"https://api.weixin.qq.com/cgi-bin/media/upload?access_token={access_token}&type=image";
     6             byte[] data = Convert.FromBase64String(b64);
     7             var boundary = "fbce142e-4e8e-4bf3-826d-cc3cf506cccc";
     8             var client = new HttpClient();
     9             client.DefaultRequestHeaders.Add("User-Agent", "KnowledgeCenter");
    10             client.DefaultRequestHeaders.Remove("Expect");
    11             client.DefaultRequestHeaders.Remove("Connection");
    12             client.DefaultRequestHeaders.ExpectContinue = false;
    13             client.DefaultRequestHeaders.ConnectionClose = true;
    14             var content = new MultipartFormDataContent(boundary);
    15             content.Headers.Remove("Content-Type");
    16             content.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary);
    17             var contentByte = new ByteArrayContent(data);
    18             content.Add(contentByte);
    19             contentByte.Headers.Remove("Content-Disposition");
    20             contentByte.Headers.TryAddWithoutValidation("Content-Disposition", $"form-data; name="media";filename="{Guid.NewGuid()}.png"" + "");
    21             contentByte.Headers.Remove("Content-Type");
    22             contentByte.Headers.TryAddWithoutValidation("Content-Type", "image/png");
    23             try
    24             {
    25                 var result2 = client.PostAsync(url, content);
    26                 if (result2.Result.StatusCode != HttpStatusCode.OK)
    27                     throw new Exception(result2.Result.Content.ReadAsStringAsync().Result);
    28                 string jsonstr = result2.Result.Content.ReadAsStringAsync().Result;
    29                 var dic = Newtonsoft.Json.Linq.JObject.Parse(jsonstr);
    30                 return dic["media_id"].ToString();
    31             }
    32             catch (Exception ex)
    33             {
    34                 return null;
    35             }
    36         }        
  • 相关阅读:
    UI5 Databind
    the meaning of myconputer environment path
    Linux查看日志常用命令
    fw: IP bonding in Linux.
    FW:expect tcl install
    install ET underlinux
    转expect 文一篇。
    linux 集萃
    try expect and autoexpect
    linux 压缩种类
  • 原文地址:https://www.cnblogs.com/grax/p/13100825.html
Copyright © 2011-2022 走看看