微信公众平台对接C#-上传永久媒体文件

#region 上传永久媒体文件
        /// <summary>
        ///上传永久媒体文件
        /// </summary>
        public class UploadForeverMediaReModel
        {
            public string type { get; set; }
            public string media_id { get; set; }
            public string url { get; set; }
            public string errcode { get; set; }
            public string errmsg { get; set; }
        }
        public static string UploadForeverMedia(string file, string MsgType, string accessToken, string guid, string LogName)
        {
            FileInfo fileinfo = new FileInfo(file);
            T9.Util.LogUtil.WriteLog("上传文件:" + file, LogName);
            //(image): 2M,支持bmp/png/jpeg/jpg/gif格式
            //(voice):2M,播放长度不超過60s,mp3/wma/wav/amr格式
            //(video):10MB,支持MP4格式
            //(thumb):64KB,支持JPG格式​
            string fileName = file;
            string url = string.Format("https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={0}&type={1}", accessToken, MsgType);
            FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read);
            byte[] fileByte = new byte[fs.Length];
            fs.Read(fileByte, 0, fileByte.Length);
            fs.Close();
            // 設置參數
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            CookieContainer cookieContainer = new CookieContainer();
            request.CookieContainer = cookieContainer;
            request.AllowAutoRedirect = true;
            request.Method = "POST";
            string boundary = DateTime.Now.Ticks.ToString("X"); // 隨機分隔線
            request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
            byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
            byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
            StringBuilder sbHeader =
                new StringBuilder(
                    string.Format(
                        "Content-Disposition:form-data;name=\"media\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileinfo.Name
                        ));
            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
            Stream postStream = request.GetRequestStream();
            postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
            postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
            postStream.Write(fileByte, 0, fileByte.Length);
            #region 新增描述信息
            if (MsgType == "video")
            {
                byte[] tempboundary = Encoding.UTF8.GetBytes("\r\n\r\n--" + boundary + "\r\n");
                postStream.Write(tempboundary, 0, tempboundary.Length);
                byte[] temp1 = Encoding.UTF8.GetBytes("\r\nContent-Disposition:form-data;name=\"description\";\r\nContent-Type:application/json\r\n\r\n");
                byte[] temp2 = Encoding.UTF8.GetBytes("{\"title\":\"标题\",\"introduction\":\"内容\"}");
                postStream.Write(temp1, 0, temp1.Length);
                postStream.Write(temp2, 0, temp2.Length);
            }
            #endregion
            postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
            postStream.Close();
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            Stream instream = response.GetResponseStream();
            StreamReader sr = new StreamReader(instream, Encoding.UTF8);
            string content = sr.ReadToEnd();
            T9.Util.LogUtil.WriteLog("UploadMedia-returnJson:" + content, guid, LogName);
            UploadForeverMediaReModel result = T9.Util.JSONUtil.JsonDeserialize<UploadForeverMediaReModel>(content);
            if (string.IsNullOrWhiteSpace(result.errcode))
            {
                return result.media_id;
            }
            return "";
        }
        #endregion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

可曾听闻丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值