Example #1
0
        /// <SUMMARY>
        /// 上传多媒体文件,返回 MediaId
        /// </SUMMARY>
        /// <PARAM name="ACCESS_TOKEN"></PARAM>
        /// <PARAM name="Type"></PARAM>
        /// <RETURNS></RETURNS>
        public string UploadMultimedia(string ACCESS_TOKEN, string Type, string filepath)
        {
            string result = "";
            string wxurl  = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=" + ACCESS_TOKEN + "&type=" + Type;
            //string filepath = System.Web.HttpContext.Current.Server.MapPath("image") + "\\hemeng80.jpg"; //(本地服务器的地址)
            //string filepath = "D:\\site\\b2betown\\ETS2.WebApp\\UploadFile\\2014060012433370369.jpg";
            //TxtHelper.CheckLog("上传路径:" + filepath, "D:\\logg.txt");
            WebClient myWebClient = new WebClient();

            myWebClient.Credentials = CredentialCache.DefaultCredentials;
            try
            {
                byte[] responseArray = myWebClient.UploadFile(wxurl, "POST", filepath);
                result = System.Text.Encoding.Default.GetString(responseArray, 0, responseArray.Length);
                //TxtHelper.CheckLog("上传result:" + result, "D:\\wxuploadlog.txt");
                UploadMM _mode = JsonConvert.DeserializeObject <UploadMM>(result);
                result = _mode.media_id;
                //TxtHelper.CheckLog("上传MediaId:" + result, "D:\\wxuploadlog.txt");
            }
            catch (Exception ex)
            {
                result = "";
                //TxtHelper.CheckLog("上传MediaId失败:" + ex.Message, "D:\\wxuploadlog.txt");
            }
            return(result);
        }
Example #2
0
        /// <summary>
        /// 上传图文消息素材(微信群发),返回media_id
        /// </summary>
        /// <param name="articles"></param>
        /// <returns></returns>
        public string Uploadnews(string ACCESS_TOKEN, List <Wxarticle> articles, out string created_at)
        {
            if (articles == null)
            {
                created_at = "";
                return("");
            }
            if (articles.Count == 0)
            {
                created_at = "";
                return("");
            }
            string requesturl = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=" + ACCESS_TOKEN;

            Wxarticles obj = new Wxarticles();

            obj.articles = articles;

            string json_param = JsonConvert.SerializeObject(obj);

            #region
            //System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();

            //StringBuilder json_param = new StringBuilder();
            //jss.Serialize(obj, json_param);

            //string json_param = "{\"articles\": [";
            //foreach (Wxarticle m in articles)
            //{
            //    json_param += "{" +
            //            "\"thumb_media_id\":\"" + m.thumb_media_id + "\"," +
            //            "\"author\":\"" + m.author + "\"," +
            //             "\"title\":\"" + m.title + "\"," +
            //             "\"content_source_url\":\"" + m.content_source_url + "\"," +
            //             "\"content\":\"" + m.content + "\"," +
            //             "\"digest\":\"" + m.digest + "\"," +
            //            "\"show_cover_pic\":\"" + m.show_cover_pic + "\"" +
            //            "},";
            //}
            //json_param = json_param.Substring(0, json_param.Length - 1);
            //json_param += "]}";
            #endregion
            try
            {
                string ret = new GetUrlData().HttpPost(requesturl, json_param.ToString());
                JavaScriptSerializer ser = new JavaScriptSerializer();
                UploadMM             foo = ser.Deserialize <UploadMM>(ret);
                created_at = foo.created_at;

                return(foo.media_id);
            }
            catch
            {
                created_at = "";
                return("");
            }
        }