最近在做关于微信公众号接口的问题,发现上传永久素材的地方有些小问题。
1.临时素材和永久素材没有关系,在微信服务器可以保存三天。新增的临时素材只能通过media_id(临时素材)获取。
2.新增图文素材以外的素材,需要使用‘新增其他类型永久素材’接口,其中文档写的地址为
http请求方式: POST,需使用https https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN
在测试的过程中发现,少了type属性。我之前一直用json对象传值进去,所以总是出现各种错误。
通过这个接口就可以获得到media_id(永久素材)。
3.上传需要使用multipart/form-data方式。
下面是代码,在网上搜了一些高手的方法,稍微进行了一下组合,亲测可用
////// 新增永久素材 /// /// /// [HttpPost] public string UploadLong(FormCollection form) { if (Request.Files.Count == 0) { //Request.Files.Count 文件数为0上传不成功 return "文件数为0上传不成功"; } var file = Request.Files[0]; if (file.ContentLength == 0) { //文件大小(以字节为单位)为0时,做一些操作 return "文件大小(以字节为单位)为0"; } else { //文件大小不为0 HttpPostedFileBase files = Request.Files[0]; string type = "image"; string url = string.Format("http://api.weixin.qq.com/cgi-bin/material/add_material?access_token={0}&type={1}", token, type.ToString()); string path = "C://Users//xjh//Desktop//img//" + files.FileName; //服务器上的UpLoadFile文件夹必须有读写权限 files.SaveAs(path); string filename = System.Web.HttpContext.Current.Server.MapPath("/image/" + files.FileName); //返回url //var geturl = HttpUploadFile(string.Format("https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={0}", token), file.FileName); string json = HttpUploadFile(url, filename); JObject jb = (JObject)JsonConvert.DeserializeObject(json); //todo return json; } }
以下为post方法
public static string HttpUploadFile(string url, string path) { // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线 request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary; byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n"); byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); int pos = path.LastIndexOf("\\"); string fileName = path.Substring(pos + 1); //请求头部信息 StringBuilder sbHeader = new StringBuilder( string.Format( "Content-Disposition:form-data;name=\"media\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName)); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString()); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); byte[] bArr = new byte[fs.Length]; fs.Read(bArr, 0, bArr.Length); fs.Close(); Stream postStream = request.GetRequestStream(); postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); postStream.Write(bArr, 0, bArr.Length); postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); postStream.Close(); //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream instream = response.GetResponseStream(); StreamReader sr = new StreamReader(instream, Encoding.UTF8); //返回结果网页(html)代码 string content = sr.ReadToEnd(); return content; }