protected void Button1_Click(object sender, EventArgs e)
{
if (File1.PostedFile.ContentLength != 0)
{
//获取 ACCESS_TOKEN
string accessjson = pub.Weixin.GetAccessToken();
M.accessstring access_model = pub.json.JsonDeserializeBySingleData<M.accessstring>(accessjson);
string ACCESS_TOKEN = access_model.access_token; //获取ACCESS_TOKEN
string type = "image";
//发送请求url
string sUrl = "https://qyapi.weixin.qq.com/cgi-bin/material/add_material?type="+ type + "&access_token=" + ACCESS_TOKEN;
if (File1.PostedFile.ContentLength!=0)
{
string filename = File1.PostedFile.FileName;
string path = Server.MapPath("./");
path += filename;
if (File.Exists(path)) {
Response.Write("文件已经存在");
return;
}
File1.PostedFile.SaveAs(path);
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
try {
//上传数据获得字符
byte[] responseArray = myWebClient.UploadFile(sUrl,"POST",path);
//转换字符
string ret = System.Text.Encoding.Default.GetString(responseArray,0,responseArray.Length);
//解析json
M.material mymaterial = pub.json.JsonDeserializeBySingleData<M.material>(ret);
Response.Write(mymaterial.media_id);
}
catch(Exception ex) {
}
}
}
}
参考网址:
http://bbs.csdn.net/topics/391014694/