分类:
微信
前言
Happy Birthday To You! ChenTingXian
人的一生会遇到很多人,但心里能够一直有你的人却很少,承蒙时光不弃,让我遇到啦你们,今天是个特别的日子,仙宝贝生日快乐
今天学啦微信图片与视频的上传,发送关键字,就可以出现指定的视频或图片
先来说一下图片的上传
<a href="/Upload/Img">图片上传</a> <a href="/Upload/Vedio">视频上传</a>
后台封装你微信的token,便于后期好调用
public static string GetToken() { RedisClient redisClient = new RedisClient(); ////先从缓存获取 //string token = redisClient.Get<string>("token"); //if (token != null) // return token; HttpClient httpClient = new HttpClient(); string result = httpClient.GetAsync("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxc2b251cc0434ec05&secret=7c7ca1ee2063ae8415a94d85f1239667"). Result.Content.ReadAsStringAsync().Result; ObtainDTO obtainDTO = JsonConvert.DeserializeObject<ObtainDTO>(result); //把token写入缓存 redisClient.Set<string>("token", obtainDTO.Access_token, TimeSpan.FromSeconds(obtainDTO.Expires_in - 120)); return obtainDTO.Access_token; }
获取后的token,调用路径的时候需要用到
@{ string token = WXChatPublic_DAL.ObtainDAL.GetToken(); } <form method="post" action="https://api.weixin.qq.com/cgi-bin/media/upload?access_token=@token&type=image" enctype="multipart/form-data"> <input type="file" name="fileimg"/> <input type="submit" value="上传"/>
接下来就是视频的上传啦,具体步骤都跟图片的大同小异
@{ string token = WXChatPublic_DAL.ObtainDAL.GetToken(); } <h2>Vedio</h2> <form id="upform" method="post" action="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=@token&type=video" enctype="multipart/form-data"> <input type="file" name="media"/> <input type="text" name="description" id="description" style="width:100%" /> <input type="button" onclick="upvedio()" value="上传" /> </form> <script> var upvedio = function () { var upform = document.getElementById("upform"); //视频描述 var description = document.getElementById("description"); var jsonobj = {}; jsonobj.title = "小视频"; jsonobj.introduction = "微信小视频"; description.value = JSON.stringify(jsonobj); upform.submit(); } </script>
读取你上传的图片或视频
//图片
public static string SendImg(string toUser, string fromUser, string MediaId) { string xml = string.Format(@"<xml> <ToUserName><![CDATA[{0}]]></ToUserName> <FromUserName><![CDATA[{1}]]></FromUserName> <CreateTime>{2}</CreateTime> <MsgType><![CDATA[image]]></MsgType> <Image> <MediaId><![CDATA[{3}]]></MediaId> </Image> </xml> ", fromUser, toUser, GetWxTime(), MediaId); xml = xml.Trim(); return xml; }
//视频
public static string SendVedio(string toUser, string fromUser, string MediaId) { string xml = string.Format(@"<xml> <ToUserName><![CDATA[{0}]]></ToUserName> <FromUserName><![CDATA[{1}]]></FromUserName> <CreateTime>{2}</CreateTime> <MsgType><![CDATA[video]]></MsgType> <Video> <MediaId><![CDATA[{3}]]></MediaId> <Title><![CDATA[视频]]></Title> <Description><![CDATA[微信视频]]></Description> </Video> </xml> ", fromUser, toUser, GetWxTime(), MediaId); xml = xml.Trim(); return xml; }
//接收微信推送的xml
StreamReader streamReader = new StreamReader(Request.InputStream); string xml = streamReader.ReadToEnd(); WXLog.Write(xml, "", "记录微信推送内容"); XElement xElement = XElement.Parse(xml); //发送者 string FromUserName = xElement.Element("FromUserName").Value; //开发者微信号 string ToUserName = xElement.Element("ToUserName").Value; //消息类型 string MsgType = xElement.Element("MsgType").Value; string result = ""; if (MsgType.ToLower() == "text")//文本消息 { string Content = xElement.Element("Content").Value; if (Content == "redis")//关键字消息 { result = ReadNews.SendText(ToUserName, FromUserName); } else if (Content == "图片")//关键字消息 { result = ReadNews.SendImg(ToUserName, FromUserName,"KBBGrhxaIrflD2vL0BiqIrH_IAfbj2_HQHxymsUcrAT2sb5UBdWxZGqV3v17htgM"); } else if (Content == "视频")//关键字消息 { result = ReadNews.SendVedio(ToUserName, FromUserName, "CjktGqG6rCX3ln48DSQo6fIBibIRxqC4GkrjctuX9IQ"); } else { result = ReadNews.SendMsg(ToUserName, FromUserName, Content); } }
摇摆摇摆
评价