62,046
社区成员
发帖
与我相关
我的任务
分享
WebClient wxUpload = new WebClient();
string uploadPath = "~/Images/";
string folder = Server.MapPath(uploadPath);
//自动创建目录
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
HttpPostedFile file = Request.Files.Get(0);
string filename = folder + file.FileName;
file.SaveAs(filename);
//API所需的媒体信息
wxUpload.Headers.Add("Content-Type", file.ContentType);
wxUpload.Headers.Add("filename", file.FileName);
wxUpload.Headers.Add("filelength", file.ContentLength.ToString());
byte[] result =
wxUpload.UploadFile(
new Uri(string.Format(
"https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={0}&type={1}",
AnalysisXmlMsg.GetAccessToken("appid", "appsecret"),
"image")), filename);
string resultjson = Encoding.UTF8.GetString(result); //在这里获取json数据,获得图片URL
using (WebClient client = new WebClient())
{
byte[] b = client.UploadFile(url, filepath);
string retdata = Encoding.Default.GetString(b);
if (retdata.Contains("media_id"))
{
return JsonConvert.DeserializeObject<UpLoadInfo>(retdata);
}
else
{
Message.Insert(new Message(MessageType.Exception, retdata));
return null;
}
}