/// <summary> /// 检测上传的媒体是否满足基本要求 /// </summary> private WeChatResult Check(LocalMedia media) { var sizes = new Dictionary <string, long> { { Media.Image, 128 * 1024 }, { Media.Voice, 256 * 1024 }, { Media.Video, 1 * 1024 * 1024 }, { Media.Thumb, 64 * 1024 }, }; if (media == null) { return(WeChatResult.GetFailed("缺少媒体参数")); } if (!File.Exists(media.MediaPath)) { return(WeChatResult.GetFailed("指定的媒体文件不存在")); } if (string.IsNullOrEmpty(media.MediaType)) { return(WeChatResult.GetFailed("未指定媒体类型")); } if (new FileInfo(media.MediaPath).Length > sizes[media.MediaType]) { return(WeChatResult.GetFailed(string.Format("指定的媒体文件超过限制大小{0}K", sizes[media.MediaPath] / 1024))); } return(WeChatResult.GetSuccess()); }
/// <summary> /// /// </summary> public RemoteMedia Upload(LocalMedia media) { var checkRet = Check(media); if (!checkRet.IsSuccess) throw new WxException(-9999, checkRet.ErrMsg); var param = new HttpParam { {"access_token", Token.GetAccessToken(media)}, {"type", media.MediaType} }; var rs = HttpHelper.Upload(ApiList.MediaUploadUrl, param, media.MediaPath); var result = JsonConvert.DeserializeObject<RemoteMedia>(rs); if (string.IsNullOrEmpty(result.MediaID)) { var ex = JsonConvert.DeserializeObject<WeChatResult>(rs); throw new WxException(ex); } return result; }
/// <summary> /// 检测上传的媒体是否满足基本要求 /// </summary> private WeChatResult Check(LocalMedia media) { var sizes = new Dictionary<string, long> { {Media.Image, 128*1024}, {Media.Voice, 256*1024}, {Media.Video, 1*1024*1024}, {Media.Thumb, 64*1024}, }; if (media == null) return WeChatResult.GetFailed("缺少媒体参数"); if (!File.Exists(media.MediaPath)) return WeChatResult.GetFailed("指定的媒体文件不存在"); if (string.IsNullOrEmpty(media.MediaType)) return WeChatResult.GetFailed("未指定媒体类型"); if (new FileInfo(media.MediaPath).Length > sizes[media.MediaType]) return WeChatResult.GetFailed(string.Format("指定的媒体文件超过限制大小{0}K", sizes[media.MediaPath] / 1024)); return WeChatResult.GetSuccess(); }
/// <summary> /// /// </summary> public RemoteMedia Upload(LocalMedia media) { var checkRet = Check(media); if (!checkRet.IsSuccess) { throw new WxException(-9999, checkRet.ErrMsg); } var param = new HttpParam { { "access_token", Token.GetAccessToken(media) }, { "type", media.MediaType } }; var rs = HttpHelper.Upload(ApiList.MediaUploadUrl, param, media.MediaPath); var result = JsonConvert.DeserializeObject <RemoteMedia>(rs); if (string.IsNullOrEmpty(result.MediaID)) { var ex = JsonConvert.DeserializeObject <WeChatResult>(rs); throw new WxException(ex); } return(result); }