上傳臨時素材

public JsonResult Temporary_UploadPhoto()
{
//創建一個文件夾
string uploadPath = "~/image/";
string flieder = Server.MapPath(uploadPath);

//判斷圖片是否已經存在
if (!Directory.Exists(flieder))
{
Directory.CreateDirectory(flieder);
}


//獲取上傳
var file = Request.Files.Get(0);


//獲取文件路徑名
string filename = flieder + file.FileName;


//保存上傳文件的內容
file.SaveAs(filename);


//設置標頭值
WebClient wc = new WebClient();
wc.Headers.Add("Content-Type", file.ContentType);
wc.Headers.Add("filename", file.FileName);
wc.Headers.Add("filelength", file.ContentLength.ToString());

//獲取access_token
string token = MvcApplication1.Tools.Access_token_Tool.access_token;

//上傳臨時素材的接口

string wxurl = string.Format("https://api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}", token, "image");


byte[] result = wc.UploadFile(new Uri(wxurl), filename);

return Json(Encoding.UTF8.GetString(result));


}
發佈了32 篇原創文章 · 獲贊 5 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章