#region 上传永久媒体文件
/// <summary>
///上传永久媒体文件
/// </summary>
public class UploadForeverMediaReModel
{
public string type { get; set; }
public string media_id { get; set; }
public string url { get; set; }
public string errcode { get; set; }
public string errmsg { get; set; }
}
public static string UploadForeverMedia(string file, string MsgType, string accessToken, string guid, string LogName)
{
FileInfo fileinfo = new FileInfo(file);
T9.Util.LogUtil.WriteLog("上传文件:" + file, LogName);
//(image): 2M,支持bmp/png/jpeg/jpg/gif格式
//(voice):2M,播放长度不超過60s,mp3/wma/wav/amr格式
//(video):10MB,支持MP4格式
//(thumb):64KB,支持JPG格式
string fileName = file;
string url = string.Format("https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={0}&type={1}", accessToken, MsgType);
FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read);
byte[] fileByte = new byte[fs.Length];
fs.Read(fileByte, 0, fileByte.Length);
fs.Close();
// 設置參數
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");
StringBuilder sbHeader =
new StringBuilder(
string.Format(
"Content-Disposition:form-data;name=\"media\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileinfo.Name
));
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
Stream postStream = request.GetRequestStream();
postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
postStream.Write(fileByte, 0, fileByte.Length);
#region 新增描述信息
if (MsgType == "video")
{
byte[] tempboundary = Encoding.UTF8.GetBytes("\r\n\r\n--" + boundary + "\r\n");
postStream.Write(tempboundary, 0, tempboundary.Length);
byte[] temp1 = Encoding.UTF8.GetBytes("\r\nContent-Disposition:form-data;name=\"description\";\r\nContent-Type:application/json\r\n\r\n");
byte[] temp2 = Encoding.UTF8.GetBytes("{\"title\":\"标题\",\"introduction\":\"内容\"}");
postStream.Write(temp1, 0, temp1.Length);
postStream.Write(temp2, 0, temp2.Length);
}
#endregion
postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
postStream.Close();
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream instream = response.GetResponseStream();
StreamReader sr = new StreamReader(instream, Encoding.UTF8);
string content = sr.ReadToEnd();
T9.Util.LogUtil.WriteLog("UploadMedia-returnJson:" + content, guid, LogName);
UploadForeverMediaReModel result = T9.Util.JSONUtil.JsonDeserialize<UploadForeverMediaReModel>(content);
if (string.IsNullOrWhiteSpace(result.errcode))
{
return result.media_id;
}
return "";
}
#endregion