Media

    /// <summary>
    ///  上传/下载媒体基类
    /// </summary>
    public class Media
    {
        /// <summary>
        /// 调用接口凭证 
        /// </summary>
        public string access_token { get; set; }

        /// <summary>
        /// 媒体文件类型，分别有图片（image）、语音（voice）、视频（video）和缩略图（thumb） 
        /// </summary>
        public BaseInfoTYPE type { get; set; }

        /// <summary>
        /// 完整路径+文件名
        /// </summary>
        public string fileName { get; set; }

        /// <summary>
        /// 下载时用到
        /// </summary>
        public string mediaId { get; set; }


        /// <summary>
        /// 上传媒体文件
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="type"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public  UploadResultJson Upload()
        {
            var url = string.Format("http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}", access_token, type.ToString());
            var fileDictionary = new Dictionary<string, string>();
            fileDictionary["media"] = fileName;
            return HttpUtility.Post.PostFileGetJson<UploadResultJson>(url, null, fileDictionary, null);
        }


        /// <summary>
        /// 下载多媒体文件,至少要实例化access_token，type，fileName，mediaId三个属性
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="mediaId"></param>
        /// <param name="fileName">完整路径E:\testpic_{0}.jpg</param>
        /// <returns></returns>
        public bool DownloadMedia()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                var url = string.Format("http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={0}&media_id={1}",
                 access_token, mediaId);
                Get.Download(url, ms);
                if (ms.Length < 1)
                {
                    return false;
                }
                using (FileStream fs = new FileStream(fileName, FileMode.Create))
                {
                    ms.Position = 0;
                    byte[] buffer = new byte[1024];
                    int bytesRead = 0;
                    while ((bytesRead = ms.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        fs.Write(buffer, 0, bytesRead);
                    }
                    fs.Flush();
                }

            }
            return File.Exists(fileName);
        }



    }