根据素材Id,来保存微信素材返回的语音,图片和视频地址

 

微信公众号的api参考地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738730

很多人没留意微信api的文档,例如获取图片素材列表的时候,也有返回了图片地址,不过那个是网络类型的,运营到自己的网站会显示引用来自微信,看不了,要在这调用微信的获取素材api调用一次,把图片下载下来才可以

/// <summary>
/// 根据素材Id查图片 appId:微信appId   selectType:查询类型 accesstoken:accesstoken media_id:素材Id
/// </summary>
/// <param name="appId">appId</param>
/// <param name="selectType">查询类型</param>
/// <param name="accesstoken">accesstoken</param>
/// <param name="media_id">media_id</param>
/// <returns></returns>
private MaterialDetailApi GetMaterialImgUrlById(string appId, string selectType, string accesstoken, string media_id)
{
var ret = new MaterialDetailApi();
try
{


var url = $"https://api.weixin.qq.com/cgi-bin/material/get_material?access_token={accesstoken}";
var contentValue = new
{
media_id = media_id
};
var wc = new HttpClient();

var requestJsons = JsonConvert.SerializeObject(contentValue);
HttpContent httpContent = new StringContent(requestJsons);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
httpContent.Headers.ContentType.CharSet = "utf-8";
var returns = wc.PostAsync(url, httpContent).Result.Content;
if (selectType == "news"||selectType == "image")//图文和图片返回的是图片的二进制
{
byte[] acstrReturn = returns.ReadAsByteArrayAsync().Result;
System.IO.MemoryStream ms = new MemoryStream(acstrReturn);//转换成无法调整大小的MemoryStream对象
var bitmap = new Bitmap(ms);//将MemoryStream对象转换成Bitmap对象
var filePath = HttpContext.Current.Server.MapPath("/uploadeFiles/Menu/img");
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
var picPath = HttpContext.Current.Server.MapPath("/uploadeFiles/Menu/img/") + media_id + ".jpg";
bitmap.Save(picPath, System.Drawing.Imaging.ImageFormat.Jpeg);
ret.path = UpLoadeUrl.Upurl() + "/uploadeFiles/Menu/img/" + media_id + ".jpg";
}
else if(selectType == "voice")//音频返回的是音频的二进制
{
var fileName = returns.Headers.ContentDisposition.FileName.ToString();
var strlist = fileName.Split(.);
var type= strlist[1].Replace("\"","");
var file = HttpContext.Current.Server.MapPath("/uploadeFiles/Menu/voice");
if (!Directory.Exists(file))
{
Directory.CreateDirectory(file);
}
var filePath = HttpContext.Current.Server.MapPath("/uploadeFiles/Menu/voice/") + media_id + "." + type;
ret.path =UpLoadeUrl.Upurl() + "/uploadeFiles/Menu/voice/" + media_id + "." + type;
ret.format = type;
byte[] acstrReturn = returns.ReadAsByteArrayAsync().Result; 
FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
fs.Write(acstrReturn, 0, acstrReturn.Length);
fs.Close();


}

else //视频的返回的是对象,里面包括图片地址
{
var acstrReturn = returns.ReadAsStringAsync().Result; 
JavaScriptSerializer acjs = new JavaScriptSerializer();
dynamic acmodelDy = acjs.Deserialize<dynamic>(acstrReturn); //反序列化 
ret.down_url = acmodelDy["down_url"];
ret.description = acmodelDy["description"];
ret.title = acmodelDy["title"];

}

 

//if (paths.Contains("jxb8"))
//{
// paths = paths.Replace("http://", "https://");
//}
}
catch(Exception ex)
{

}
return ret;

}

 

根据素材Id,来保存微信素材返回的语音,图片和视频地址

上一篇:基类和派生类小程序--简单


下一篇:小程序自定义头部(支持自定义返回)