Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of permanent material Management in asp.net Wechat Development

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "sample analysis of permanent material management in asp.net Wechat development", which is easy to understand and well organized. I hope it can help you solve your doubts. Now let the editor lead you to study and learn about "sample analysis of permanent material management in asp.net Wechat development".

In addition to temporary material that expires in 3 days, developers sometimes need to keep some material permanently, and then they can add permanent material through this API.

Recently updated, after the permanent image material is added, the URL will be returned to the developer, and the developer can use it within the Tencent domain name (for use outside the Tencent domain name, the image will be blocked).

Please note:

1. The new permanent material can also be found in the material management module of the official website of the public platform.

2. There is an upper limit on the amount of permanent material, please add it carefully. The upper limit of picture and text message material and picture material is 5000, and other types are 1000.

3. The format and size of the material is consistent with the official website of the public platform. Specifically, the image size is no more than 2m, bmp/png/jpeg/jpg/gif format is supported, voice size is not more than 5m, length is no more than 60 seconds, and mp3/wma/wav/amr format is supported.

4. Https protocol is required to call this API.

First, let's take a look at my own custom image of permanent material management in the background, as follows:

Then take a look at the backstage display interface on Wechat's official website, synchronized!

First, let's analyze the steps:

Step 1: if you want the picture to be displayed on your own page, you must first create an entity class to store information about the material.

/ / Wechat permanent material entity class, which is used to save the data returned after the permanent material is uploaded to the Wechat server / public class WxSuCaiInfo {public int SuCaiId {get; set;} / / the sequence number public string SuCaiUrl {get; set;} / / the storage file name public string SuCaiType {get; set;} / / material type, which can be divided into image,voice,video,thumb (thumbnail) public string SuCaiTitle {get; set } / / the title of the teletext message public string SuCaiDigest {get; set;} / / the summary of the teletext message public string SuCaiauthor {get; set;} / / the author of the teletext message public string SuCaishow_cover_pic {get; set;} / / whether the teletext message shows the cover. Save the body content of 0 or 1 public string SuCaicontent {get; set;} / / the body content of the picture message public string SuCaicontent_source_url {get; set;} / / the original link of the picture message public string media_ID {get; set;} / / uploads to the Wechat server, the returned permanent mediaID public string Url {get; set;} / / uploads to the Wechat server, the returned picture URL, and only the picture returns this attribute public string uploadDate {get; set } / / upload date and time}

Step 2: upload the image to the Wechat server. After success, save the returned media_id and url field data and other field data to the local server. The upload code is as follows:

/ / upload the picture to the Wechat server, and save a local copy of / protected void LinBtnUploadImg_Click (object sender, EventArgs e) {if (this.FileUploadImage.HasFile) {string fileContentType = FileUploadImage.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.FileUploadImage.PostedFile.ContentLength; if (fileSize 0) {Response.Write ("alert ('picture material uploaded successfully!') ; location='WxSuCaiMannageImageList.aspx'; ");} else {Response.Write (" alert ('failed to upload picture material!') ; location='WxSuCaiMannageImageList.aspx'; ");}} catch (Exception ex) {Response.Write (ex.Message.ToString ());}} else {Response.Write (" alert ('upload files cannot be larger than 2MB') ") ;}} else {Response.Write ("alert ('only pictures in BMP,GIF,PNG,JPG,JPEG format!')") ;} else {Response.Write ("alert ('Please select a picture!')") ;}}

As a matter of fact, the effect has already come out. Next, the last step is to delete the selected material, delete the data from the Wechat remote server-- and then delete the data from the local server. Some people ask, is there any order in this?

In fact, you can imagine that if the pictures on the Wechat server are not deleted successfully, if you delete the pictures on the local server first, you will not be able to synchronize with the official website.

Step 3: delete the material

/ / Select all / protected void CheckAll_CheckedChanged (object sender, EventArgs e) {foreach (DataListItem item in this.DLSuCaiImageList.Items) {CheckBox checkIn = item.FindControl ("CheckIn") as CheckBox; checkIn.Checked = CheckAll.Checked;}} / delete the selected item / protected void LinkBtnDeleteSelected_Click (object sender, EventArgs e) {Boolean ischeck = false Foreach (DataListItem item in this.DLSuCaiImageList.Items) {CheckBox checkIn = item.FindControl ("CheckIn") as CheckBox; if (checkIn.Checked) {ischeck = true; Label lbSuCaiId = item.FindControl ("lbSuCaiId") as Label; Label lbSuCaiUrl = item.FindControl ("lbSuCaiUrl") as Label; Label lbmedia_ID = item.FindControl ("lbmedia_ID") as Label; / / Delete the picture on the Wechat server WeiXinServer wxs = new WeiXinServer (); string res = "" / / read accesstoken string Access_token = Cache ["Access_token"] as string; if (Access_token = = null) {/ / if empty, retrieve Access_token = wxs.GetAccessToken (); / / set cached data to expire after 7000 seconds 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/del_material?access_token=" + Access_tokento; / / POST data example: POST data example: {" media_id ": MEDIA_ID} string media_id = lbmedia_ID.Text.ToString () String postData = "{\" media_id\ ":\" + media_id + "\"} "; res = wxs.GetPage (posturl, postData); if (res.Contains (" errcode ")) {/ / start parsing the json string. You need to refer to the Newtonsoft.json.dll file JObject jsonObj = JObject.Parse (res) before use. If (jsonObj ["errcode"]. ToString (). Equals ("0") {/ get the path of the local server string serverPathss = Server.MapPath ("~ / WeiXinImg/") + lbSuCaiUrl.Text.ToString (); / / verify whether the image if (File.Exists (serverPathss)) {/ / delete File.Delete (serverPathss) if it exists;} WxSuCaiService wscs = new WxSuCaiService () / / Delete local server database records through media_id int num = wscs.DeleteWxSuCaiInfo (lbmedia_ID.Text.ToString ()); if (num > 0) {Response.Write ("alert ('picture material deleted successfully!') ; location='WxSuCaiMannageImageList.aspx'; ");} else {Response.Write (" alert ('Wechat server image deleted successfully! Failed to delete local server image material!') ; location='WxSuCaiMannageImageList.aspx'; ");}} if (! ischeck) {ScriptManager.RegisterClientScriptBlock (this.Page, this.GetType (),", "alert ('Please select the deleted item first!')" , true); return;}}

Finally, the code of the page is presented together for careful study.

.meun {width:1100px; height:40px; margin-left:20px; line-height:40px; margin-top:10px;border-bottom:1px solid # d6d6d6;} .meun ul {padding:0px; margin:0px;} .meun ul li {float:left; width:100px; text-align:center;list-style:none;} .meun ul li:hover {border-bottom:3px solid # ecd9df; cursor:pointer;} a:hover {color:#000;} .checkedstyle {border-bottom:3px solid # 208008 } .meun _ imglist {width:1050px; min-height:300px; border:1px solid # d6d6d6; margin-top:20px; margin-left:35px; margin-bottom:30px;} .uploadstyle {width:300px; background-image:url ('images/inputbg.gif'); background-repeat:repeat-x; height:35px; border:1px solid # d6d6d6; float:left; margin-bottom:10px; line-height:35px;} .CheckAll {float:left; padding:5px;} .Checkin {float:left; padding:2px DLSuCaiImageList {margin-top:10px; margin-left:10px;} location: home Wechat Management Deqiao employee Service Center-material Management

Material management permanent material and Wechat official website synchronization, you operate here any one, will affect the official website background material management, careful operation!

Picture and text message picture library voice video

Jpg,gif,png,bmp format is supported for uploading pictures with a size of less than 2m. If the picture cannot be displayed after a successful upload, please rename the picture and try to upload it again.

Select all delete select

20?Eval ("uploadDate") .ToString () .Substring (0pion20) + "...": Eval ("uploadDate") .ToString ()% >

Other material uploads are similar, so they will not be introduced one by one.

The interface of the newly created picture and text material is as follows:

Select the picture material from the picture library as follows:

This is selected from the uploaded picture library, which is basically similar to the function of the picture material management interface, except that there is one more button to confirm the selection, because after confirming the selection, you need to close this page and go back to the new picture and text page. the main code:

Confirm the selection. Once selected, jump to the new image and text page / protected void LinkBtnSubMitSelected_Click (object sender, EventArgs e) {Boolean bools = false; int num = 0; foreach (DataListItem item in this.DLSuCaiImageList.Items) {CheckBox checkIn = item.FindControl ("CheckIn") as CheckBox; if (checkIn.Checked) {num + = 1; bools = true }} if (! bools) {ScriptManager.RegisterClientScriptBlock (this.Page, this.GetType (), "", "alert ('Please select a picture material!')" , true); return;} if (num > = 2) {ScriptManager.RegisterClientScriptBlock (this.Page, this.GetType (), "", "alert ('you can only select one picture material!') ; ", true); return;} else {foreach (DataListItem item in this.DLSuCaiImageList.Items) {CheckBox checkIn = item.FindControl (" CheckIn ") as CheckBox; if (checkIn.Checked) {/ get the selected picture media_id Label lbmedia_ID = item.FindControl (" lbmedia_ID ") as Label; Session [" imgmedia_id "] = lbmedia_ID.Text.ToString (); Response.Write (" alert ('selected!') ; window.opener.location.reload (); window.close (); ");}}

A page that creates a new picture and text can be received as follows:

If (Session ["imgmedia_id"]! = null) {WxSuCaiService wscs = new WxSuCaiService (); WxSuCaiInfo wscinfo = wscs.GetWxSuCaiInfo (Session ["imgmedia_id"]. ToString ()); if (wscinfooted null) {this.ImgTuWen.ImageUrl = "~ / WeiXinImg/" + wscinfo.SuCaiUrl.ToString (); this.ImgTuWen2.ImageUrl = "~ / WeiXinImg/" + wscinfo.SuCaiUrl.ToString (); this.ImgTuWen2.Visible = true Session ["imgmedia_id"] = wscinfo.media_ID.ToString (); / / media_id Session of the picture ["fileNameimg"] = wscinfo.SuCaiUrl.ToString (); / / File name of the picture}}

Finally, the effect of the new image and text information is as follows:

The official backstage is as follows:

The key code for editing picture and text information is as follows:

/ binding event / protected void DLMpNewsList_ItemDataBound (object sender, DataListItemEventArgs e) {if (e.Item.ItemType==ListItemType.Item | | e.Item.ItemType==ListItemType.AlternatingItem) {LinkButton LinkBtnDeleteSucai = e.Item.FindControl ("LinkBtnDeleteSucai") as LinkButton; LinkBtnDeleteSucai.Attributes.Add ("OnClick", "return confirm ('are you sure you want to delete this picture and text material? delete it at the same time as the official website of Wechat!')) ; HyperLink HyperLinkEdit = e.Item.FindControl ("HyperLinkEdit") as HyperLink; HyperLinkEdit.Attributes.Add ("OnClick", "return confirm ('about to enter edit mode!! do you want to do the next step?')) ; Label lbmedia_ID = e.Item.FindControl ("lbmedia_ID") as Label; HyperLinkEdit.NavigateUrl = "WxNewTuWen.aspx?media_id=" + lbmedia_ID.Text.ToString (); / / transfer the media_id of the message to the new interface}}

The key codes for creating a new image and text page are as follows:

If (! Page.IsPostBack) {/ / Edit mode if (Request.QueryString ["media_id"]! = null) {string media_id = Request.QueryString ["media_id"] .ToString (); Session ["sucaimedia_id"] = media_id; WxSuCaiService wscs = new WxSuCaiService (); WxSuCaiInfo wscinfo = wscs.GetWxSuCaiInfo (media_id); if (wscinfo! = null) {this.txttuwen_title.Value = wscinfo.SuCaiTitle.ToString () If (wscinfo.SuCaiTitle.ToString (). Length > 15) {this.biaoti_yulan.InnerText = wscinfo.SuCaiTitle.ToString (). Substring (0,15) + "...";} else {this.biaoti_yulan.InnerText = wscinfo.SuCaiTitle.ToString ();} this.txttuwen_author.Value = wscinfo.SuCaiauthor.ToString (); this.txtzhaiyao.InnerText = wscinfo.SuCaiDigest.ToString (); this.ImgTuWen.ImageUrl = "~ / WeiXinImg/" + wscinfo.SuCaiUrl.ToString () This.ImgTuWen2.ImageUrl = "~ / WeiXinImg/" + wscinfo.SuCaiUrl.ToString (); this.ImgTuWen2.Visible = true; Session ["imgmedia_id"] = wscinfo.SuCaithumb_media_id.ToString (); this.LinkBtnDeleteImg.Visible = true; if (! String.IsNullOrWhiteSpace (wscinfo.SuCaicontent_source_url.ToString () {this.txtYuanWenUrl.Text = wscinfo.SuCaicontent_source_url.ToString (); this.txtYuanWenUrl.Visible = true; this.CheckYuanWen.Checked = true } this.txtYuanWenUrl.Text = wscinfo.SuCaicontent_source_url.ToString (); this.tbContent.InnerText = wscinfo.SuCaicontent.ToString (); if (wscinfo.SuCaishow_cover_pic.ToString (). Equals ("1")) {this.CheckFengMianShow.Checked = true;} else {this.CheckFengMianShow.Checked = false;}}

The key codes for editing and submitting are as follows:

/ Save Picture and text material and modify buttons Common / protected void LinkBtnSaveYongjiu_Click (object sender, EventArgs e) {/ / non-empty verification if (String.IsNullOrWhiteSpace (this.txttuwen_title.Value.ToString () {ScriptManager.RegisterClientScriptBlock (this.Page, this.GetType (), "," alert ('Please enter the title!') ; ", true); return;} if (this.ImgTuWen2.ImageUrl.ToString () .Equals (")) {ScriptManager.RegisterClientScriptBlock (this.Page, this.GetType (), "", "alert ('a picture must be uploaded!') ; ", true); return;} if (String.IsNullOrWhiteSpace (this.tbContent.InnerText.ToString () {ScriptManager.RegisterClientScriptBlock (this.Page, this.GetType (),", "alert ('Please enter the body content!') ; ", true); return;} / / assign each item WeiXinServer wxs = new WeiXinServer (); / / read accesstoken string Access_token = Cache [" Access_token "] as string; if (Access_token = = null) {/ / if it is empty, retrieve Access_token = wxs.GetAccessToken () / / set cached data to expire after 7000 seconds ("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) / / determine whether media_id is empty according to session You can also determine whether if is empty according to request.queryString ["media_id"] (Session ["sucaimedia_id"]! = null) {/ / perform update operation / / {/ / "media_id": MEDIA_ID, / / "index": INDEX, / / "articles": {/ / "title": TITLE, / / "thumb_media_id": THUMB_MEDIA_ID, / / "author": AUTHOR / / "digest": DIGEST, / / "show_cover_pic": SHOW_COVER_PIC (0 / 1), / / "content": CONTENT, / / "content_source_url": CONTENT_SOURCE_URL / /} /} string isshow_cover_pic = "" If (this.CheckFengMianShow.Checked) {isshow_cover_pic = "1";} else {isshow_cover_pic = "0";} string description = NoHTML (this.tbContent.InnerText.ToString ()) String postData = "{\" media_id\ ":\" + Session ["sucaimedia_id"] .ToString () + ",\" index\ ":\" 0\ ",\" articles\ ": {\" title\ ":\" + this.txttuwen_title.Value.ToString () + "\",\ "thumb_media_id\":\ "+ Session [" imgmedia_id "] .ToString () +"\ " \ "author\":\ "+ this.txttuwen_author.Value.ToString () +"\ ",\" digest\ ":\"+ this.txtzhaiyao.InnerText.ToString () +"\ ",\" show_cover_pic\ ":\"+ isshow_cover_pic +"\ ",\" content\ ":\" + description + "\",\ "content_source_url\":\ "+ this.txtYuanWenUrl.Text.ToString () +"\ "}" / / modify permanent graphic material string url = string.Format ("https://api.weixin.qq.com/cgi-bin/material/update_news?access_token={0}", Access_tokento); string jsonres = PostUrl (url, postData); if (jsonres.Contains (" errcode ")) {/ / need to refer to the Newtonsoft.json.dll file JObject jsonObj = JObject.Parse (jsonres) before use If (jsonObj ["errcode"]. ToString (). Equals ("0") {/ / modify the local data / / save the data to facilitate reading WxSuCaiInfo wsc = new WxSuCaiInfo () from the local server when getting the list; wsc.SuCaiUrl = Session ["fileNameimg"]. ToString (); / / notice that the name of the picture saved here is wsc.SuCaiTitle = this.txttuwen_title.Value.ToString () / / the title of the picture-text message wsc.SuCaiDigest = this.txtzhaiyao.InnerText.ToString (); / / the summary of the picture-text message wsc.SuCaithumb_media_id = Session ["imgmedia_id"]. ToString (); / / the message cover of the picture and text media_id wsc.SuCaiauthor = this.txttuwen_author.Value.ToString (); wsc.SuCaishow_cover_pic = isshow_cover_pic; wsc.SuCaicontent = description; wsc.SuCaicontent_source_url = this.txtYuanWenUrl.Text.ToString () Wsc.uploadDate = System.DateTime.Now.ToString (); / record the date and time of saving the picture and text material in the current file / / modify the database information WxSuCaiService wscs = new WxSuCaiService (); int num = wscs.UpdateWxSuCaiInfo (Session ["sucaimedia_id"]. ToString (), wsc); if (num > 0) {Session ["sucaimedia_id"] = null; Response.Write ("alert ('picture and text material modified successfully!') ; location='WxSuCaiManageList.aspx'; ");} else {Response.Write (" alert ('failed to modify picture and text material!') ; ");}} else {/ / add picture and text material}}

Note: create a new page and modify the page is a common page.

The edit submit button and the save button are common buttons.

The above is all the contents of the article "sample Analysis of permanent material Management in asp.net Wechat Development". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report