只有生成素材ID,图片才能在微信中以图片格式发出去。
/// <summary> /// 获取素材ID /// </summary> /// <param name="type"></param> /// <param name="filepath"></param> /// <returns></returns> public static string GetMaterial(string type, string filepath) { string Materialpath = System.AppDomain.CurrentDomain.BaseDirectory + filepath; Entity Material = WeixinDA.GetMaterial(Materialpath); DateTime Time = Material.GetValue("OverTime").ToDateTime(); int days = (Time - DateTime.Now).Days; //超时时间和当前时间相比 if (days > 0)//有效期内 { return Material.GetValue("MaterialID").TryString(); } else { return UpMaterial(type, Materialpath); } } /// <summary> /// 上传临时素材,获得mediaID /// </summary> /// <param name="type"> 上传的临时多媒体文件。格式和大小限制,如下: /// 图片(image): 1M,支持JPG格式 /// 语音(voice):2M,播放长度不超过60s,支持AMRMP3格式 /// 视频(video):10MB,支持MP4格式 /// 缩略图(thumb):64KB,支持JPG格式。 /// 媒体文件在后台保存时间为3天,即3天后media_id失效。</param> /// <param name="filepath"></param> /// <returns></returns> ///public const string Material_Url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}"; public static string UpMaterial(string type, string filepath) { string accessToken = GetAccsss_token(); string url = string.Format(Material_Url, accessToken, type); WebClient myWebClient = new WebClient(); string MediaId = ""; myWebClient.Credentials = CredentialCache.DefaultCredentials; try { byte[] responseArray = myWebClient.UploadFile(url, "POST", filepath); string result = System.Text.Encoding.Default.GetString(responseArray, 0, responseArray.Length); MediaId = JObject.Parse(result)["media_id"].TryString(); } catch (Exception ex) { FileLog.AddLog("素材MaterialError", ex.Message); } return MediaId; }