MaterialAPI.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*--------------------------------------------------------------------------
  2. * MaterialAPI.cs
  3. *Auth:deepleo
  4. * Date:2015.07.25
  5. * Email:2586662969@qq.com
  6. * Website:http://www.weixinsdk.net
  7. *--------------------------------------------------------------------------*/
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Net.Http;
  13. using Codeplex.Data;
  14. using System.IO;
  15. using LYFZ.Weixin.SDK.Helpers;
  16. namespace LYFZ.Weixin.SDK
  17. {
  18. /// <summary>
  19. /// 对应微信API的 "素材管理"
  20. /// </summary>
  21. public class MaterialAPI
  22. {
  23. /// <summary>
  24. /// 新增临时素材/上传多媒体文件
  25. /// http://mp.weixin.qq.com/wiki/5/963fc70b80dc75483a271298a76a8d59.html
  26. /// 1.上传的媒体文件限制:
  27. ///图片(image) : 1MB,支持JPG格式
  28. ///语音(voice):1MB,播放长度不超过60s,支持MP4格式
  29. ///视频(video):10MB,支持MP4格式
  30. ///缩略图(thumb):64KB,支持JPG格式
  31. ///2.媒体文件在后台保存时间为3天,即3天后media_id失效
  32. /// </summary>
  33. /// <param name="access_token"></param>
  34. /// <param name="type">媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)</param>
  35. /// <param name="fileName">文件名</param>
  36. /// <param name="inputStream">文件输入流</param>
  37. /// <returns>media_id</returns>
  38. public static string UploadTempMedia(string access_token, string type, string fileName, Stream inputStream)
  39. {
  40. var url = string.Format("http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}", access_token, type.ToString());
  41. var result = DynamicJson.Parse(Util.HttpRequestPost(url, "media", fileName, inputStream));
  42. inputStream.Close();
  43. inputStream.Dispose();
  44. return result.media_id;
  45. }
  46. /// <summary>
  47. /// 获取临时素材/下载多媒体文件
  48. /// http://mp.weixin.qq.com/wiki/11/07b6b76a6b6e8848e855a435d5e34a5f.html
  49. /// 公众号可以使用本接口获取临时素材(即下载临时的多媒体文件)。请注意,视频文件不支持https下载,调用该接口需http协议。
  50. /// 本接口即为原“下载多媒体文件”接口。
  51. /// </summary>
  52. /// <param name="savePath"></param>
  53. /// <param name="access_token"></param>
  54. /// <param name="media_id"></param>
  55. public static void DownloadTempMedia(string savePath, string access_token, string media_id)
  56. {
  57. var url = string.Format("http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={0}&media_id={1}", access_token, media_id);
  58. FileStream fs = new FileStream(savePath, FileMode.Create);
  59. Util.Download(url, fs);
  60. fs.Close();
  61. fs.Dispose();
  62. }
  63. }
  64. }