微信公众平台对接C#-上传临时文件

#region 上传临时文件
        /// <summary>
        ///上传临时文件
        /// </summary>
        public class Medial
        {
            public string type { get; set; }
            public string media_id { get; set; }
            public string created_at { get; set; }
        }
        /// <summary>
        /// 上传临时文件获取mediaID
        /// </summary>
        /// <param name="ACCESS_TOKEN"></param>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public static string UploadMedia(string ACCESS_TOKEN, string filepath, string MsgType, string guid, string LogName)
        {
            T9.Util.LogUtil.WriteLog("上传图片:" + filepath, LogName);
            guid = Guid.NewGuid().ToString();
            string returnJson = "";
            string wxurl = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=" + ACCESS_TOKEN + "&type=" + MsgType;
            System.Net.WebClient myWebClient = new System.Net.WebClient();

            myWebClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
            try
            {
                byte[] responseArray = myWebClient.UploadFile(wxurl, "POST", filepath);
                returnJson = System.Text.Encoding.Default.GetString(responseArray, 0, responseArray.Length);
                T9.Util.LogUtil.WriteLog("UploadMedia-returnJson:" + returnJson, guid, LogName);
                if (!returnJson.Contains("errcode"))
                {
                    Medial result = T9.Util.JSONUtil.JsonDeserialize<Medial>(returnJson);
                    return result.media_id;
                }
                return "";
            }
            catch (Exception ex)
            {
                T9.Util.LogUtil.WriteLog(ex.Message + "\r\n" + ex.StackTrace, guid, LogName);
                return "";
            }
        }
        #endregion

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可曾听闻丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值