您现在的位置是:网站首页> 编程资料编程资料

asp.net微信开发(高级群发图文)_实用技巧_

2023-05-24 272人已围观

简介 asp.net微信开发(高级群发图文)_实用技巧_

上一篇介绍了如何群发文本消息,本篇将介绍如何群发图文信息,上传图文信息所需的素材,界面如下:

我们先看从素材库中获取图文素材的代码,界面:

素材列表,我是使用的repeater控件,

前台代码如下:

选择素材新建图文素材  
确认选择
刷新
删除素材
  • <%# Eval("title") %>
本类型素材总数量为:   本次获取的素材数量为:

后台代码如下:

 ///  /// 绑定图文素材列表 ///  private void BindNewsSucaiList() { WeiXinServer wxs = new WeiXinServer(); string res = ""; ///从缓存读取accesstoken string Access_token = Cache["Access_token"] as string; if (Access_token == null) { //如果为空,重新获取 Access_token = wxs.GetAccessToken(); //设置缓存的数据7000秒后过期 Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration); } string Access_tokento = Access_token.Substring(17, Access_token.Length - 37); string posturl = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + Access_tokento; //POST数据例子: POST数据例子:{"type":TYPE,"offset":OFFSET,"count":COUNT} string postData = "{\"type\":\"news\",\"offset\":\"0\",\"count\":\"20\"}"; res = wxs.GetPage(posturl, postData); //使用前需要引用Newtonsoft.json.dll文件 JObject jsonObj = JObject.Parse(res); int groupsnum = jsonObj["item"].Count(); List newssucaiitemlist = new List(); List WxNewsSuCaiItemlist = new List(); for (int i = 0; i < groupsnum; i++) { WxNewsSucaiIteminfo newssucaiitem = new WxNewsSucaiIteminfo(); newssucaiitem.media_id = jsonObj["item"][i]["media_id"].ToString(); newssucaiitem.update_time = jsonObj["item"][i]["update_time"].ToString(); newssucaiitem.total_count = jsonObj["total_count"].ToString(); newssucaiitem.item_count = jsonObj["item_count"].ToString(); newssucaiitemlist.Add(newssucaiitem); int news_itemcount = jsonObj["item"][i]["content"]["news_item"].Count(); if (news_itemcount > 0) { for (int j = 0; j < news_itemcount; j++) { WxNewsSuCaiItemlistinfo wnscilinfo = new WxNewsSuCaiItemlistinfo(); wnscilinfo.title = jsonObj["item"][i]["content"]["news_item"][j]["title"].ToString(); wnscilinfo.thumb_media_id = jsonObj["item"][i]["content"]["news_item"][j]["thumb_media_id"].ToString(); wnscilinfo.show_cover_pic = int.Parse(jsonObj["item"][i]["content"]["news_item"][j]["show_cover_pic"].ToString()); wnscilinfo.author = jsonObj["item"][i]["content"]["news_item"][j]["author"].ToString(); wnscilinfo.digest = jsonObj["item"][i]["content"]["news_item"][j]["digest"].ToString(); wnscilinfo.content = jsonObj["item"][i]["content"]["news_item"][j]["content"].ToString(); wnscilinfo.url = jsonObj["item"][i]["content"]["news_item"][j]["url"].ToString(); wnscilinfo.content_source_url = jsonObj["item"][i]["content"]["news_item"][j]["content_source_url"].ToString(); wnscilinfo.media_id = newssucaiitem.media_id.ToString(); WxNewsSuCaiItemlist.Add(wnscilinfo); } } } Session["WxNewsSuCaiItemlist"] = WxNewsSuCaiItemlist; this.Repeatersucailist.DataSource = newssucaiitemlist; this.Repeatersucailist.DataBind(); } 

再来看看,新建单图文信息界面:

新建单图文上传封面,删除封面的代码如下:

 ///  /// /// 上传图片文件 ///  ///  protected void LinkBtnFileUploadImg_Click(object sender, EventArgs e) { if (this.FileUploadImg.HasFile) { string fileContentType = FileUploadImg.PostedFile.ContentType; if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/png" || fileContentType == "image/x-png" || fileContentType == "image/jpeg" || fileContentType == "image/pjpeg") { int fileSize = this.FileUploadImg.PostedFile.ContentLength; if (fileSize <=2097152) { string fileName = this.FileUploadImg.PostedFile.FileName; // 客户端文件路径 string filepath = FileUploadImg.PostedFile.FileName; //得到的是文件的完整路径,包括文件名,如:C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg //string filepath = FileUpload1.FileName; //得到上传的文件名20022775_m.jpg string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);//20022775_m.jpg string serverpath = Server.MapPath("~/WeiXinImg/") + filename;//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg this.ImgTuWen.ImageUrl = "~/WeiXinImg/" + FileUploadImg.FileName; this.ImgTuWen2.Visible = true; this.ImgTuWen2.ImageUrl = "~/WeiXinImg/" + FileUploadImg.FileName; this.FileUploadImg.PostedFile.SaveAs(serverpath);//将上传的文件另存为 this.LinkBtnDeleteImg.Visible = true; Session["fileNameimg"] = this.FileUploadImg.PostedFile.FileName; //上传临时图片素材至微信服务器,3天后微信服务器会自动删除 WeiXinServer wxs = new WeiXinServer(); ///从缓存读取accesstoken string Access_token = Cache["Access_token"] as string; if (Access_token == null) { //如果为空,重新获取 Access_token = wxs.GetAccessToken(); //设置缓存的数据7000秒后过期 Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration); } string Access_tokento = Access_token.Substring(17, Access_token.Length - 37); //WebClient wx_upload = new WebClient(); //wx_upload.Credentials = CredentialCache.DefaultCredentials; string url = string.Format("http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}", Access_tokento, "image"); string result = HttpUploadFile(url, serverpath); if (result.Contains("media_id")) { //使用前需要引用Newtonsoft.json.dll文件 JObject jsonObj = JObject.Parse(result); Session["imgmedia_id"] = jsonObj["media_id"].ToString(); } Response.Write(""); } else { Response.Write(""); } } else { Response.Write(""); } } else { Response.Write(""); } } ///  /// Http上传文件 ///  public static string HttpUploadFile(string url, string path) { // 设置参数 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"); int pos = path.LastIndexOf("\\"); string 
                
                

-六神源码网