\r\n \r\n \r\n 位置:<\/span>\r\n \r\n  
  • 首頁<\/a><\/li>\r\n  
  • 微信管理<\/li>\r\n  
  • 德橋員工服務(wù)中心--素材管理<\/li>\r\n <\/ul>\r\n <\/p>\r\n  素材管理<\/span>永久素材和微信官網(wǎng)同步,您在這里所操作的任何一項(xiàng),將影響到官網(wǎng)后臺素材管理,謹(jǐn)慎操作!<\/span><\/p>\r\n \r\n  
      \r\n  
    • 圖文消息<\/a><\/li>\r\n  圖片庫<\/a><\/li>\r\n  
    • 語音<\/a><\/li>\r\n  
    • 視頻<\/a><\/li>\r\n  <\/ul>\r\n <\/p>\r\n \r\n  \r\n   \r\n  上傳<\/span><\/asp:LinkButton>\r\n    支持jpg,gif,png,bmp格式圖片,大小2M內(nèi),如上傳成功后,圖片未能顯示,請將圖片重新命名后再嘗試上傳.<\/span>\r\n  <\/p>\r\n  \r\n    全選<\/span>\r\n  刪除選中<\/span><\/asp:LinkButton>\r\n  <\/p>\r\n  \r\n  \r\n  \r\n   \r\n   \"Introduction'  style=\"max-width:90%\" \/>\r\n   \r\n    \r\n    ' runat=\"server\" Text='<%# Eval(\"SuCaiUrl\").ToString().Length>8?Eval(\"SuCaiUrl\").ToString().Substring(0,8)+\"...\":Eval(\"SuCaiUrl\").ToString() %>'><\/asp:Label>\r\n   <\/p>\r\n   \r\n    <%# Eval(\"uploadDate\").ToString().Length>20?Eval(\"uploadDate\").ToString().Substring(0,20)+\"...\":Eval(\"uploadDate\").ToString() %>\r\n    '><\/asp:Label>\r\n    '><\/asp:Label>\r\n   <\/p>\r\n   <\/p>\r\n  <\/ItemTemplate>\r\n  <\/asp:DataList>\r\n\r\n <\/p>\r\n <\/form>\r\n<\/body>\r\n<\/html><\/pre>

      <\/p>

      其他素材上傳都類似,就不一一介紹了。新建圖文素材界面如下:<\/p>

      \"Introduction<\/p>

      從圖片庫選擇圖片素材如下:<\/p>

      \"Introduction<\/p>

      這是就是從已上傳過的圖片庫中選擇的,和圖片素材管理界面的功能基本相似,只不過多了一個(gè)確認(rèn)選擇的按鈕,因?yàn)榇_認(rèn)選擇了之后,要關(guān)閉本頁,回到新建圖文頁面,主要代碼:<\/p>

      <\/p>

       \/\/\/ \r\n \/\/\/ 確認(rèn)選擇,選中之后,跳轉(zhuǎn)至新建圖文頁面\r\n \/\/\/ <\/summary>\r\n \/\/\/ <\/param>\r\n \/\/\/ <\/param>\r\n protected void LinkBtnSubMitSelected_Click(object sender, EventArgs e)\r\n {\r\n\r\n  Boolean bools = false;\r\n  int num = 0;\r\n  foreach (DataListItem item in this.DLSuCaiImageList.Items)\r\n  {\r\n  CheckBox checkIn = item.FindControl(\"CheckIn\") as CheckBox;\r\n\r\n  if (checkIn.Checked)\r\n  {\r\n   num += 1;\r\n   bools = true;\r\n  }\r\n  }\r\n  if (!bools)\r\n  {\r\n  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), \"\", \"alert('請選擇一個(gè)圖片素材?。。?#39;)\", true);\r\n  return;\r\n  }\r\n  if (num >= 2)\r\n  {\r\n  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), \"\", \"alert('您只能選擇一個(gè)圖片素材!');\", true);\r\n  return;\r\n  }\r\n  else\r\n  {\r\n  foreach (DataListItem item in this.DLSuCaiImageList.Items)\r\n  {\r\n   CheckBox checkIn = item.FindControl(\"CheckIn\") as CheckBox;\r\n\r\n   if (checkIn.Checked)\r\n   {\r\n   \/\/\/獲取選中圖片media_id\r\n   Label lbmedia_ID = item.FindControl(\"lbmedia_ID\") as Label;\r\n   Session[\"imgmedia_id\"] = lbmedia_ID.Text.ToString();\r\n   Response.Write(\"
      	
      
      
      
      
      
      
      

      亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

      Home WeChat Applet WeChat Development Introduction to permanent material management in asp.net WeChat development

      Introduction to permanent material management in asp.net WeChat development

      Mar 10, 2017 pm 02:38 PM

      這篇文章講述Introduction to permanent material management in asp.net WeChat development的相關(guān)內(nèi)容,需要的朋友可以參考下

      除了3天就會失效的臨時(shí)素材外,開發(fā)者有時(shí)需要永久保存一些素材,屆時(shí)就可以通過本接口新增永久素材。

      最近更新,永久圖片素材新增后,將帶有URL返回給開發(fā)者,開發(fā)者可以在騰訊系域名內(nèi)使用(騰訊系域名外使用,圖片將被屏蔽)。

      請注意:

      • 1、新增的永久素材也可以在公眾平臺官網(wǎng)素材管理模塊中看到

      • 2、永久素材的數(shù)量是有上限的,請謹(jǐn)慎新增。圖文消息素材和圖片素材的上限為5000,其他類型為1000

      • 3、素材的格式大小等要求與公眾平臺官網(wǎng)一致。具體是,圖片大小不超過2M,支持bmp/png/jpeg/jpg/gif格式,語音大小不超過5M,長度不超過60秒,支持mp3/wma/wav/amr格式

      • 4、調(diào)用該接口需https協(xié)議

      先來看我自己自定義的后臺永久素材管理效果圖,如下:

      Introduction to permanent material management in asp.net WeChat development

      再看看微信官網(wǎng)后臺上的顯示界面,同步的哦!

      Introduction to permanent material management in asp.net WeChat development

      首先我們來分析一下步驟:
      第一步:如果想讓圖片在自己的頁面顯示,首先得先建個(gè)實(shí)體類吧,用來存儲素材的信息吧


      /// <summary>
       /// 微信永久素材實(shí)體類,用于保存永久素材上傳至微信服務(wù)器后返回的數(shù)據(jù)
       /// </summary>
       public class WxSuCaiInfo
       {
       public int SuCaiId { get; set; }//自增列序號
      
       public string SuCaiUrl { get; set; }// 存儲文件名
      
       public string SuCaiType { get; set; }//素材類型,可分為image,voice,video,thumb(縮略圖)
      
       public string SuCaiTitle { get; set; }//圖文消息的標(biāo)題
      
       public string SuCaiDigest { get; set; }//圖文消息的摘要
      
      
       public string SuCaiauthor { get; set; }//圖文消息的作者
       public string SuCaishow_cover_pic { get; set; }//圖文消息是否顯示封面.保存0或1
      
       public string SuCaicontent { get; set; }//圖文消息的正文內(nèi)容
       public string SuCaicontent_source_url { get; set; }//圖文消息的原文鏈接
      
      
       public string media_ID { get; set; }//上傳至微信服務(wù)器后,返回的永久mediaID
      
      
       public string Url { get; set; }//上傳至微信服務(wù)器后,返回的圖片URL,僅圖片才會返回此屬性
      
       public string uploadDate { get; set; }//上傳日期時(shí)間
      
      
       }


      第二步:上傳圖片至微信服務(wù)器,成功后將返回的media_id和url兩個(gè)字段數(shù)據(jù)和其他字段數(shù)據(jù)一并保存到本地服務(wù)器,上傳的代碼如下:


       /// <summary>
       /// 上傳圖片至微信服務(wù)器,并且本地也保存一份
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       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 <= 2097152)
         {
         string fileName = this.FileUploadImage.PostedFile.FileName;
         // 客戶端文件路徑
         string filepath = FileUploadImage.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;//取得文件在服務(wù)器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg
      
         //把圖片上傳至本地服務(wù)器
         this.FileUploadImage.PostedFile.SaveAs(serverpath);//將上傳的文件另存為
      
      
         //上傳圖片素材至微信服務(wù)器,永久保存
      
         WeiXinServer wxs = new WeiXinServer();
      
         ///從緩存讀取accesstoken
         string Access_token = Cache["Access_token"] as string;
      
         if (Access_token == null)
         {
          //如果為空,重新獲取
          Access_token = wxs.GetAccessToken();
      
          //設(shè)置緩存的數(shù)據(jù)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 url = string.Format("http://api.weixin.qq.com/cgi-bin/material/add_material?access_token={0}&type={1}", Access_tokento,"image");
      
         try
         {
      
          string res = HttpUploadFile(url, serverpath);
          //判斷res結(jié)果集里面是否包含media_id
          if (res.Contains("media_id"))
          {
          //如果能進(jìn)行到這里,那說明圖片已經(jīng)上傳至微信服務(wù)器,是永久素材哦,
          //開始解析json串,使用前需要引用Newtonsoft.json.dll文件
          JObject jsonObj = JObject.Parse(res);
      
          
      
          //圖片上傳成功后,返回的是兩個(gè)字段,media_id和url
      
      
          //將兩個(gè)字段開始存入數(shù)據(jù)庫,保存數(shù)據(jù),方便獲取列表的時(shí)候直接從本地服務(wù)器讀取
          WxSuCaiInfo wsc = new WxSuCaiInfo();
      
          wsc.SuCaiUrl = filename;//注意,這里保存的圖片名稱
      
          wsc.SuCaiType = "image";//文件類型
      
          wsc.media_ID = jsonObj["media_id"].ToString();//這個(gè)屬性保存的是微信返回的media_id
      
          wsc.Url = jsonObj["url"].ToString();//這個(gè)屬性保存的才是微信返回的url
      
          wsc.uploadDate = System.DateTime.Now.ToString();//記錄當(dāng)前文件上傳日期時(shí)間
      
          //存入數(shù)據(jù)庫
          WxSuCaiService wscs = new WxSuCaiService();
      
      
          int num = wscs.AddWxSuCaiInfo(wsc);
      
          if (num > 0)
          {
           Response.Write("<script>alert(&#39;上傳圖片素材成功!&#39;);location=&#39;WxSuCaiMannageImageList.aspx&#39;;</script>");
          }
          else
          {
           Response.Write("<script>alert(&#39;上傳圖片素材失??!&#39;);location=&#39;WxSuCaiMannageImageList.aspx&#39;;</script>");
          }
          }
         }
         catch(Exception ex)
         {
          Response.Write(ex.Message.ToString());
         }
         }
         else
         {
         Response.Write("<script>alert(&#39;上傳文件不能大于2M!&#39;)</script>");
         }
      
        }
        else
        {
         Response.Write("<script>alert(&#39;只支持BMP,GIF,PNG,JPG,JPEG格式的圖片!&#39;)</script>");
        }
        }
        else
        {
        Response.Write("<script>alert(&#39;請選擇圖片!&#39;)</script>");
        }
       }


      走到這其實(shí)效果已經(jīng)出來了,接下來看最后一步就是刪除選中的素材,刪除微信遠(yuǎn)程服務(wù)器的數(shù)據(jù)--再刪除本地服務(wù)器的數(shù)據(jù),有人問難道這個(gè)還有順序?
      其實(shí)你可以想象,如果微信服務(wù)器的圖片沒有刪除成功,你先把本地服務(wù)器的圖片刪除了,那就和官網(wǎng)同步不了了。
      第三步:刪除素材


       /// <summary>
       /// 全選全不選
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       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;
        }
       }
       /// <summary>
       /// 刪除選中項(xiàng)
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       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;
         
         //刪除微信服務(wù)器上的圖片
         WeiXinServer wxs = new WeiXinServer();
         string res = "";
      
         ///從緩存讀取accesstoken
         string Access_token = Cache["Access_token"] as string;
      
         if (Access_token == null)
         {
         //如果為空,重新獲取
         Access_token = wxs.GetAccessToken();
      
         //設(shè)置緩存的數(shù)據(jù)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/del_material?access_token=" + Access_tokento;
      
      
         //POST數(shù)據(jù)例子: POST數(shù)據(jù)例子:{"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"))
         {
         //開始解析json串,使用前需要引用Newtonsoft.json.dll文件
         JObject jsonObj = JObject.Parse(res);
      
         if (jsonObj["errcode"].ToString().Equals("0"))
         {
      
          ///獲取本地服務(wù)器的路徑
          string serverPathss = Server.MapPath("~/WeiXinImg/") + lbSuCaiUrl.Text.ToString();
          //驗(yàn)證本地服務(wù)的路徑是否存在該圖片
          if (File.Exists(serverPathss))
          {
          //如果存在就刪除
          File.Delete(serverPathss);
          }
      
          WxSuCaiService wscs = new WxSuCaiService();
          //通過media_id刪除本地服務(wù)器數(shù)據(jù)庫記錄
          int num = wscs.DeleteWxSuCaiInfo(lbmedia_ID.Text.ToString());
          if (num > 0)
          {
          Response.Write("<script>alert(&#39;圖片素材刪除成功!&#39;);location=&#39;WxSuCaiMannageImageList.aspx&#39;;</script>");
          }
          else
          {
          Response.Write("<script>alert(&#39;微信服務(wù)器圖片刪除成功!本地服務(wù)器圖片素材刪除失??!&#39;);location=&#39;WxSuCaiMannageImageList.aspx&#39;;</script>");
          }
         }
         } 
        }
        }
        if (!ischeck)
        {
        ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;請先選中刪除項(xiàng)!??!&#39;)", true);
        return;
        }
       }


      最后是頁面的代碼一并奉上,大家仔細(xì)研究。


      <!DOCTYPE html>
      
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
       <title></title>
       <link href="css/style.css" rel="Stylesheet" type="text/css" />
       <style type="text/css">
       .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(&#39;images/inputbg.gif&#39;); 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;
       }
       </style>
      </head>
      <body>
       <form id="form1" runat="server">
       <p class="place">
       <span>位置:</span>
       <ul class="placeul">
        <li><a href="WelCome.aspx" target="rightFrame">首頁</a></li>
        <li>微信管理</li>
        <li>德橋員工服務(wù)中心--素材管理</li>
       </ul>
       </p>
        <p style="height:30px; line-height:30px; margin-top:10px; margin-left:45px;"><span style="float:left; font-size:16px;">素材管理</span><span style="color:red; float:left; margin-left:20px;">永久素材和微信官網(wǎng)同步,您在這里所操作的任何一項(xiàng),將影響到官網(wǎng)后臺素材管理,謹(jǐn)慎操作!</span></p>
       <p class="meun">
        <ul>
        <li><a href="WxSuCaiManageList.aspx">圖文消息</a></li>
        <li class="checkedstyle"><a href="WxSuCaiMannageImageList.aspx">圖片庫</a></li>
        <li><a href="#">語音</a></li>
        <li><a href="#">視頻</a></li>
        </ul>
       </p>
       <p class="meun_imglist">
        <p style="margin:5px auto 10px 10px; height:36px; line-height:36px;">
        <asp:FileUpload ID="FileUploadImage" CssClass="uploadstyle" runat="server" /> 
        <asp:LinkButton ID="LinBtnUploadImg" runat="server" OnClick="LinBtnUploadImg_Click"><span style="background-image:url(&#39;images/buttonbg.png&#39;); width:111px; height:35px; line-height:35px; margin-bottom:10px; font-weight:bold; text-align:center; float:left; margin-left:10px; color:#fff;">上傳</span></asp:LinkButton>
        <span style="margin-left:30px; color:red;" >  支持jpg,gif,png,bmp格式圖片,大小2M內(nèi),如上傳成功后,圖片未能顯示,請將圖片重新命名后再嘗試上傳.</span>
        </p>
        <p style=" clear:both;line-height:35px; margin:10px auto auto auto; height:35px; width:1030px; background-color:#f6f6f6; border-radius:5px; border-bottom:1px solid #d6d6d6;">
        <asp:CheckBox ID="CheckAll" CssClass="CheckAll" AutoPostBack="true" runat="server" OnCheckedChanged="CheckAll_CheckedChanged" />  <span style="float:left; padding:3px;">全選</span>
        <asp:LinkButton ID="LinkBtnDeleteSelected" runat="server" OnClick="LinkBtnDeleteSelected_Click"><span style="width:111px; height:25px; line-height:25px; font-weight:bold; text-align:center; float:left; margin-left:15px; color:#000; background-color:#fff; margin-top:5px; border:1px solid #ecd9df; border-radius:3px;">刪除選中</span></asp:LinkButton>
        </p>
        
        <asp:DataList ID="DLSuCaiImageList" CssClass="DLSuCaiImageList" runat="server" RepeatColumns="6">
        <ItemTemplate>
         <p style="width:150px; height:180px; margin-right:22px;margin-bottom:15px; border:1px solid #d9d9d9;">
         <img  src="/static/imghw/default1.png"  data-src="https://img.php.cn/upload/article/000/000/013/27cf21ae8c49f0cd6901fe542d2b5426-2.png"  class="lazy"  src=&#39;../WeiXinImg/<%# Eval("SuCaiUrl") % alt="Introduction to permanent material management in asp.net WeChat development" >&#39;   style="max-width:90%" />
         <p style="width:150px;height:25px; line-height:25px; text-indent:3px; border-top:1px solid #d9d9d9;">
          <asp:CheckBox ID="CheckIn" CssClass="CheckIn" runat="server" />
          <asp:Label ID="lbSuCaiUrl" ToolTip=&#39;<%# Eval("SuCaiUrl")%>&#39; runat="server" Text=&#39;<%# Eval("SuCaiUrl").ToString().Length>8?Eval("SuCaiUrl").ToString().Substring(0,8)+"...":Eval("SuCaiUrl").ToString() %>&#39;></asp:Label>
         </p>
         <p style=" clear:both; width:150px;height:25px; line-height:25px; text-indent:5px; border-top:1px solid #d9d9d9;">
          <%# Eval("uploadDate").ToString().Length>20?Eval("uploadDate").ToString().Substring(0,20)+"...":Eval("uploadDate").ToString() %>
          <asp:Label ID="lbSuCaiId" runat="server" Visible="false" Text=&#39;<%# Eval("SuCaiId") %>&#39;></asp:Label>
          <asp:Label ID="lbmedia_ID" runat="server" Visible="false" Text=&#39;<%# Eval("media_ID") %>&#39;></asp:Label>
         </p>
         </p>
        </ItemTemplate>
        </asp:DataList>
      
       </p>
       </form>
      </body>
      </html>


      其他素材上傳都類似,就不一一介紹了。
      新建圖文素材界面如下:

      Introduction to permanent material management in asp.net WeChat development

      從圖片庫選擇圖片素材如下:

      Introduction to permanent material management in asp.net WeChat development

      這是就是從已上傳過的圖片庫中選擇的,和圖片素材管理界面的功能基本相似,只不過多了一個(gè)確認(rèn)選擇的按鈕,因?yàn)榇_認(rèn)選擇了之后,要關(guān)閉本頁,回到新建圖文頁面,主要代碼:


       /// <summary>
       /// 確認(rèn)選擇,選中之后,跳轉(zhuǎn)至新建圖文頁面
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       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(&#39;請選擇一個(gè)圖片素材?。?!&#39;)", true);
        return;
        }
        if (num >= 2)
        {
        ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;您只能選擇一個(gè)圖片素材!&#39;);", true);
        return;
        }
        else
        {
        foreach (DataListItem item in this.DLSuCaiImageList.Items)
        {
         CheckBox checkIn = item.FindControl("CheckIn") as CheckBox;
      
         if (checkIn.Checked)
         {
         ///獲取選中圖片media_id
         Label lbmedia_ID = item.FindControl("lbmedia_ID") as Label;
         Session["imgmedia_id"] = lbmedia_ID.Text.ToString();
         Response.Write("<script>alert(&#39;已選擇!&#39;);window.opener.location.reload();window.close();</script>");
         }
        }
        }
       }


      新建圖文的頁面在接收的時(shí)候可以這樣:


       if (Session["imgmedia_id"] != null)
         {
         WxSuCaiService wscs = new WxSuCaiService();
      
         WxSuCaiInfo wscinfo = wscs.GetWxSuCaiInfo(Session["imgmedia_id"].ToString());
      
         if(wscinfo!=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["fileNameimg"] = wscinfo.SuCaiUrl.ToString();//圖片的文件名稱
         }
      
      
         }


      最后新建圖文信息的效果圖如下:

      Introduction to permanent material management in asp.net WeChat development

      官方后臺如下:

      Introduction to permanent material management in asp.net WeChat development

      關(guān)于編輯圖文信息的關(guān)鍵代碼如下:


       /// <summary>
       /// 綁定事件
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       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(&#39;您確定刪除該圖文素材???刪除后將和微信官網(wǎng)同步刪除??!&#39;)");
      
      
        HyperLink HyperLinkEdit = e.Item.FindControl("HyperLinkEdit") as HyperLink;
      
        HyperLinkEdit.Attributes.Add("OnClick", "return confirm(&#39;即將進(jìn)入編輯模式??!是否執(zhí)行下一步操作??&#39;)");
      
      
        Label lbmedia_ID = e.Item.FindControl("lbmedia_ID") as Label;
      
      
        HyperLinkEdit.NavigateUrl = "WxNewTuWen.aspx?media_id=" + lbmedia_ID.Text.ToString();//把圖文消息的media_id傳參到新建圖文界面
      
        }
       }


      Introduction to permanent material management in asp.net WeChat development

      新建圖文頁面關(guān)鍵代碼如下:


       if(!Page.IsPostBack)
        {
        ///編輯模式
        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;
         }
         }
        }
        }


      Introduction to permanent material management in asp.net WeChat development

      編輯提交關(guān)鍵代碼如下:


      /// <summary>
       /// 保存圖文素材和修改按鈕公用
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       protected void LinkBtnSaveYongjiu_Click(object sender, EventArgs e)
       {
        //非空驗(yàn)證
        if (String.IsNullOrWhiteSpace(this.txttuwen_title.Value.ToString()))
        {
        ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;請輸入圖文標(biāo)題!&#39;);", true);
        return;
        }
        if (this.ImgTuWen2.ImageUrl.ToString().Equals(""))
        {
        ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;必須上傳一張圖片!&#39;);", true);
        return;
        }
        if (String.IsNullOrWhiteSpace(this.tbContent.InnerText.ToString()))
        {
        ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(&#39;請輸入正文內(nèi)容!&#39;);", true);
        return;
        }
      
        //對各項(xiàng)進(jìn)行賦值
        WeiXinServer wxs = new WeiXinServer();
      
        ///從緩存讀取accesstoken
        string Access_token = Cache["Access_token"] as string;
      
        if (Access_token == null)
        {
        //如果為空,重新獲取
        Access_token = wxs.GetAccessToken();
      
        //設(shè)置緩存的數(shù)據(jù)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);
      
      
        
       //根據(jù)session判斷media_id是否為空,也可根據(jù)request.queryString["media_id"]進(jìn)行判斷是否為空
        if (Session["sucaimedia_id"] != null)
        {
        //執(zhí)行更新操作
      
        //{
        // "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() +
        "\"}}";
      
      
        ///修改永久圖文素材
        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"))
        {
         //使用前需要引用Newtonsoft.json.dll文件
         JObject jsonObj = JObject.Parse(jsonres);
      
         if (jsonObj["errcode"].ToString().Equals("0"))
         {
         //修改本地?cái)?shù)據(jù)
         //保存數(shù)據(jù),方便獲取列表的時(shí)候直接從本地服務(wù)器讀取
         WxSuCaiInfo wsc = new WxSuCaiInfo();
      
         wsc.SuCaiUrl = Session["fileNameimg"].ToString();//注意,這里保存的圖片名稱
      
         wsc.SuCaiTitle = this.txttuwen_title.Value.ToString();//圖文消息的標(biāo)題
      
         wsc.SuCaiDigest = this.txtzhaiyao.InnerText.ToString();//圖文消息的摘要
      
         wsc.SuCaithumb_media_id = Session["imgmedia_id"].ToString();//圖文的消息封面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();//記錄當(dāng)前文件保存圖文素材日期時(shí)間
      
         //修改數(shù)據(jù)庫信息
         WxSuCaiService wscs = new WxSuCaiService();
      
         int num = wscs.UpdateWxSuCaiInfo(Session["sucaimedia_id"].ToString(), wsc);
      
         if (num > 0)
         {
          Session["sucaimedia_id"] = null;
          Response.Write("<script>alert(&#39;圖文素材修改成功!&#39;);location=&#39;WxSuCaiManageList.aspx&#39;;</script>");
         }
         else
         {
          Response.Write("<script>alert(&#39;圖文素材修改失??!&#39;);</script>");
         }
      
         }
      
        }
      
        }
        else
        {
        //新增圖文素材
        }
       }


      需注意:新建圖文頁面和修改圖文頁面是公用的一個(gè)頁面.......

      編輯提交按鈕和保存按鈕是公用的一個(gè)按鈕.....

      精彩專題分享:ASP.NET微信開發(fā)教程匯總,歡迎大家學(xué)習(xí)。

      以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。

      The above is the detailed content of Introduction to permanent material management in asp.net WeChat development. For more information, please follow other related articles on the PHP Chinese website!

      Statement of this Website
      The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

      Hot AI Tools

      Undress AI Tool

      Undress AI Tool

      Undress images for free

      Undresser.AI Undress

      Undresser.AI Undress

      AI-powered app for creating realistic nude photos

      AI Clothes Remover

      AI Clothes Remover

      Online AI tool for removing clothes from photos.

      Clothoff.io

      Clothoff.io

      AI clothes remover

      Video Face Swap

      Video Face Swap

      Swap faces in any video effortlessly with our completely free AI face swap tool!

      Hot Tools

      Notepad++7.3.1

      Notepad++7.3.1

      Easy-to-use and free code editor

      SublimeText3 Chinese version

      SublimeText3 Chinese version

      Chinese version, very easy to use

      Zend Studio 13.0.1

      Zend Studio 13.0.1

      Powerful PHP integrated development environment

      Dreamweaver CS6

      Dreamweaver CS6

      Visual web development tools

      SublimeText3 Mac version

      SublimeText3 Mac version

      God-level code editing software (SublimeText3)

      Hot Topics

      PHP Tutorial
      1488
      72