频道栏目
首页 > 微信学院 > 微信公众平台开发 > 正文

微信素材上传

2016-01-07 13:33:28           
收藏   我要投稿

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

namespace W.BLL.WeChat
{
    public class PermanentMaterialUploads
    {
        /// 
        /// 永久其它类型素材上传
        /// 
        ///AccessToken
        ///素材
        ///上传的数据类型
        ///针对视频文件  文件名称
        ///针对视频文件  文件描述
        /// 
        public static string MaterialUploads(string accessToken, HttpPostedFileBase isFile, string materialType, string title = "", string introduction = "")
        {
            string url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={0}";
            url = string.Format(url, accessToken);

            //设置提交表单的名称
            string formName = materialType == "video" ? "description" : "media";

            byte[] datas = null;

            #region 组合正文
            //换行
            string CRLF = "\r\n";

            //边界标识
            string Identification = "----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            try
            {

                //生成表单信息
                StringBuilder sb = new StringBuilder();
                sb.Append("--");
                sb.Append(Identification);
                sb.Append(CRLF);
                sb.Append("Content-Disposition: form-data; id=\"" + formName + "\"; name=\"" + formName + "\"; filename=\"" + isFile.FileName + "\"");
                sb.Append(CRLF);
                sb.Append("Content-Type: " + isFile.ContentType.ToLower() + "");
                sb.Append(CRLF);
                sb.Append(CRLF);

                //如果是视频数据,就添加视频标题与视频的描述信息
                if (materialType == "video")
                {
                    sb.Append("description=\"{\"title\":\"" + title + "\", \"introduction\":\"" + introduction + "\"}\"");
                    sb.Append(CRLF);
                    sb.Append(CRLF);
                }

                //头部与尾部
                string header = sb.ToString();
                string foot = CRLF + "--" + Identification + "--" + CRLF;

                //转为byte[]流
                byte[] headerbytes = Encoding.UTF8.GetBytes(header);
                byte[] footbytes = Encoding.UTF8.GetBytes(foot);

                List datalist = new List();

                //转换为btye[]数据流
                Stream stream = isFile.InputStream;
                byte[] data = new byte[stream.Length];
                stream.Read(data, 0, data.Length);

                // 设置当前流的位置为流的开始 
                stream.Seek(0, SeekOrigin.Begin);

                datalist.AddRange(headerbytes);
                datalist.AddRange(data);
                datalist.AddRange(footbytes);

                //组合完整的流数据
                datas = datalist.ToArray();
            }
            catch (Exception)
            {
                return "正文组合失败";
            } 
            #endregion

            return MaterialUpload(datas, url, Identification);
        }

        /// 
        /// 微信素材上传
        /// 
        ///
        ///
        ///正文边界
        /// 
        public static string MaterialUpload(byte[] data, string url, string Identification)
        {
            Stream responseStream;
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            if (request == null)
            {
                throw new ApplicationException(string.Format("Invalid url string: {0}", url));
            }
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            request.ContentType = "multipart/form-data; boundary=" + Identification;
            //request.ContentType = "application/x-www-form-urlencoded; boundary=" + Identification;
            request.Method = "POST";
            request.ContentLength = data.Length;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            requestStream.Close();
            try
            {
                responseStream = request.GetResponse().GetResponseStream();
            }
            catch (Exception exception)
            {
                throw exception;
            }
            string str = string.Empty;
            using (StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8")))
            {
                str = reader.ReadToEnd();
            }
            responseStream.Close();
            return str;
        }
    }
}

 

相关TAG标签 素材
上一篇:如何判断是不是微信登录浏览器
下一篇:(一)微信公众号开发基本配置
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站