走到这其实效果已经出来了,接下来看最后一步就是删除选中的素材,删除微信远程服务器的数据--再删除本地服务器的数据,有人问难道这个还有顺序?
其实你可以想象,如果微信服务器的图片没有删除成功,你先把本地服务器的图片删除了,那就和官网同步不了了。
第三步:删除素材
/// <summary> /// 全选全不选 /// </summary> /// <param></param> /// <param></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> /// 删除选中项 /// </summary> /// <param></param> /// <param></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; //删除微信服务器上的图片 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/del_material?access_token=" + Access_tokento; //POST数据例子: POST数据例子:{"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")) { ///获取本地服务器的路径 string serverPathss = Server.MapPath("~/WeiXinImg/") + lbSuCaiUrl.Text.ToString(); //验证本地服务的路径是否存在该图片 if (File.Exists(serverPathss)) { //如果存在就删除 File.Delete(serverPathss); } WxSuCaiService wscs = new WxSuCaiService(); //通过media_id删除本地服务器数据库记录 int num = wscs.DeleteWxSuCaiInfo(lbmedia_ID.Text.ToString()); if (num > 0) { Response.Write("<script>alert('图片素材删除成功!');location='https://www.jb51.net/WxSuCaiMannageImageList.aspx';</script>"); } else { Response.Write("<script>alert('微信服务器图片删除成功!本地服务器图片素材删除失败!');location='https://www.jb51.net/WxSuCaiMannageImageList.aspx';</script>"); } } } } } if (!ischeck) { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请先选中删除项!!!')", true); return; } }
最后是页面的代码一并奉上,大家仔细研究。
<!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="https://www.jb51.net/css/style.css" 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('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; } </style> </head> <body> <form runat="server"> <div> <span>位置:</span> <ul> <li><a href="https://www.jb51.net/WelCome.aspx" target="rightFrame">首页</a></li> <li>微信管理</li> <li>德桥员工服务中心--素材管理</li> </ul> </div> <div><span>素材管理</span><span>永久素材和微信官网同步,您在这里所操作的任何一项,将影响到官网后台素材管理,谨慎操作!</span></div> <div> <ul> <li><a href="https://www.jb51.net/WxSuCaiManageList.aspx">图文消息</a></li> <li><a href="https://www.jb51.net/WxSuCaiMannageImageList.aspx">图片库</a></li> <li><a href="#">语音</a></li> <li><a href="#">视频</a></li> </ul> </div> <div> <div> <asp:FileUpload CssClass="uploadstyle" runat="server" /> <asp:LinkButton runat="server"><span>上传</span></asp:LinkButton> <span > 支持jpg,gif,png,bmp格式图片,大小2M内,如上传成功后,图片未能显示,请将图片重新命名后再尝试上传.</span> </div> <div> <asp:CheckBox CssClass="CheckAll" AutoPostBack="true" runat="server" OnCheckedChanged="CheckAll_CheckedChanged" /> <span>全选</span> <asp:LinkButton runat="server"><span>删除选中</span></asp:LinkButton> </div> <asp:DataList CssClass="DLSuCaiImageList" runat="server" RepeatColumns="6"> <ItemTemplate> <div> <img src='../WeiXinImg/<%# Eval("SuCaiUrl") %>' /> <div> <asp:CheckBox CssClass="CheckIn" runat="server" /> <asp:Label ToolTip='<%# Eval("SuCaiUrl")%>' runat="server" Text='<%# Eval("SuCaiUrl").ToString().Length>8?Eval("SuCaiUrl").ToString().Substring(0,8)+"...":Eval("SuCaiUrl").ToString() %>'></asp:Label> </div> <div> <%# Eval("uploadDate").ToString().Length>20?Eval("uploadDate").ToString().Substring(0,20)+"...":Eval("uploadDate").ToString() %> <asp:Label runat="server" Visible="false" Text='<%# Eval("SuCaiId") %>'></asp:Label> <asp:Label runat="server" Visible="false" Text='<%# Eval("media_ID") %>'></asp:Label> </div> </div> </ItemTemplate> </asp:DataList> </div> </form> </body> </html>
其他素材上传都类似,就不一一介绍了。
新建图文素材界面如下:
从图片库选择图片素材如下: