tnblog
首页
视频
资源
登录

微信上传图片和视频

5933人阅读 2019/12/16 15:27 总访问:122136 评论:0 收藏:0 手机
分类: 微信

前言


Happy Birthday To You! ChenTingXian

人的一生会遇到很多人,但心里能够一直有你的人却很少,承蒙时光不弃,让我遇到啦你们,今天是个特别的日子,仙宝贝生日快乐


今天学啦微信图片与视频的上传,发送关键字,就可以出现指定的视频或图片

先来说一下图片的上传


  1. <a href="/Upload/Img">图片上传</a>
  2. <a href="/Upload/Vedio">视频上传</a>


后台封装你微信的token,便于后期好调用

  1. public static string GetToken()
  2.         {
  3.             RedisClient redisClient = new RedisClient();
  4.             ////先从缓存获取
  5.             //string token = redisClient.Get<string>("token");
  6.             //if (token != null)
  7.             //    return token;
  8.             HttpClient httpClient = new HttpClient();
  9.             string result = 
  10.                 httpClient.GetAsync("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxc2b251cc0434ec05&secret=7c7ca1ee2063ae8415a94d85f1239667").
  11.                 Result.Content.ReadAsStringAsync().Result;
  12.             ObtainDTO obtainDTO = JsonConvert.DeserializeObject<ObtainDTO>(result);
  13.             //把token写入缓存
  14.             redisClient.Set<string>("token", obtainDTO.Access_token, TimeSpan.FromSeconds(obtainDTO.Expires_in - 120));
  15.             return obtainDTO.Access_token;
  16.         }


获取后的token,调用路径的时候需要用到

  1. @{
  2.     string token = WXChatPublic_DAL.ObtainDAL.GetToken();
  3. }
  4. <form method="post" action="https://api.weixin.qq.com/cgi-bin/media/upload?access_token=@token&type=image" enctype="multipart/form-data">
  5.     <input type="file" name="fileimg"/>
  6.     <input  type="submit" value="上传"/>


接下来就是视频的上传啦,具体步骤都跟图片的大同小异

  1. @{
  2.     string token = WXChatPublic_DAL.ObtainDAL.GetToken();
  3. }
  4. <h2>Vedio</h2>
  5. <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">
  6.    <input type="file" name="media"/>
  7.     <input type="text" name="description" id="description" style="width:100%" />
  8.     <input type="button" onclick="upvedio()" value="上传" />
  9. </form>
  10. <script>
  11.     var upvedio = function () {
  12.         var upform = document.getElementById("upform");
  13.         //视频描述
  14.         var description = document.getElementById("description");
  15.        
  16.         var jsonobj = {};
  17.         jsonobj.title = "小视频";
  18.         jsonobj.introduction = "微信小视频";
  19.         description.value = JSON.stringify(jsonobj);
  20.         upform.submit();
  21.     }
  22. </script>


读取你上传的图片或视频


//图片

  1.  public static string SendImg(string toUser, string fromUser, string MediaId)
  2.         {
  3.             string xml = string.Format(@"<xml>
  4.                                       <ToUserName><![CDATA[{0}]]></ToUserName>
  5.                                       <FromUserName><![CDATA[{1}]]></FromUserName>
  6.                                       <CreateTime>{2}</CreateTime>
  7.                                       <MsgType><![CDATA[image]]></MsgType>
  8.                                       <Image>
  9.                                         <MediaId><![CDATA[{3}]]></MediaId>
  10.                                       </Image>
  11.                                     </xml> ", fromUser, toUser, GetWxTime(), MediaId);
  12.             xml = xml.Trim();
  13.             return xml;
  14.         }


//视频

  1. public static string SendVedio(string toUser, string fromUser, string MediaId)
  2.         {
  3.             string xml = string.Format(@"<xml>
  4.                                       <ToUserName><![CDATA[{0}]]></ToUserName>
  5.                                       <FromUserName><![CDATA[{1}]]></FromUserName>
  6.                                       <CreateTime>{2}</CreateTime>
  7.                                       <MsgType><![CDATA[video]]></MsgType>
  8.                                       <Video>
  9.                                         <MediaId><![CDATA[{3}]]></MediaId>
  10.                                         <Title><![CDATA[视频]]></Title>
  11.                                         <Description><![CDATA[微信视频]]></Description>
  12.                                       </Video>
  13.                                     </xml> ", fromUser, toUser, GetWxTime(), MediaId);
  14.             xml = xml.Trim();
  15.             return xml;
  16.         }

//接收微信推送的xml

  1.   StreamReader streamReader = new StreamReader(Request.InputStream);
  2.                 string xml = streamReader.ReadToEnd();
  3.                 WXLog.Write(xml, """记录微信推送内容");
  4.                 XElement xElement = XElement.Parse(xml);
  5.                 //发送者
  6.                 string FromUserName = xElement.Element("FromUserName").Value;
  7.                 //开发者微信号
  8.                 string ToUserName = xElement.Element("ToUserName").Value;
  9.                 //消息类型
  10.                 string MsgType = xElement.Element("MsgType").Value;
  11.                 string result = "";
  12.                 if (MsgType.ToLower() == "text")//文本消息
  13.                 {
  14.                     string Content = xElement.Element("Content").Value;
  15.                     if (Content == "redis")//关键字消息
  16.                     {
  17.                         result = ReadNews.SendText(ToUserName, FromUserName);
  18.                     }
  19.                     else if (Content == "图片")//关键字消息
  20.                     {
  21.                         result = ReadNews.SendImg(ToUserName, FromUserName,"KBBGrhxaIrflD2vL0BiqIrH_IAfbj2_HQHxymsUcrAT2sb5UBdWxZGqV3v17htgM");
  22.                     }
  23.                     else if (Content == "视频")//关键字消息
  24.                     {
  25.                         result = ReadNews.SendVedio(ToUserName, FromUserName, "CjktGqG6rCX3ln48DSQo6fIBibIRxqC4GkrjctuX9IQ");
  26.                     }
  27.                     else
  28.                     {
  29.                         result = ReadNews.SendMsg(ToUserName, FromUserName, Content);
  30.                     }
  31.                 }

摇摆摇摆



评价
人之因此能,是坚信能
排名
26
文章
22
粉丝
12
评论
8
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术