WeChat server to download the file to the local instance code

Time:2019-10-24

Download the file from the WeChat server to the local instance code as follows:

// download files from WeChat server to local
public JsonResult UploadRecord(string ServerId)
{
try
{
member m = base.CurrentUser;
String MSG = "success ";
bool success = true;
SiteSettingsInfo setting = base.CurrentSiteSetting;
string file = string.Empty;
string content = string.Empty;
string strpath = string.Empty;
string savepath = string.Empty;
// WeChat address
string stUrl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" + setting.WeixinToken + "&media_id=" + ServerId;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(stUrl);
req.Method = "GET";
string filename = "";
using (WebResponse wr = req.GetResponse())
{
HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
strpath = myResponse.ResponseUri.ToString();
WebClient mywebclient = new WebClient();
string folder = DateTime.Now.ToString("yyyyMMdd");
// absolute path
string uploadPath = ConfigurationManager.AppSettings["RecordPath"] + DateTime.Now.ToString("yyyyMMdd");
// new filename
filename = DateTime.Now.ToString("HHmmssfff_yyyyMMdd") + (new Random()).Next().ToString().Substring(0, 4) + ".amr";
// relative path
file = @"GetImages/Images/" + DateTime.Now.ToString("yyyyMMdd") + @"/" + filename;
savepath = uploadPath + "\" + filename;
try
{
if (!Directory.Exists(uploadPath))
{
// automatically generates folders
Directory.CreateDirectory(uploadPath);
}
mywebclient.DownloadFile(strpath, savepath);
//file = savepath;
}
catch (Exception ex)
{
savepath = ex.ToString();
}
}
string RecordPath = ConfigurationManager.AppSettings.Get("host") + file;
//return base.Json(new { success = success, recordPath = RecordPath, fileName = DateTime.Now.ToString("yyyyMMdd") + @"\" + filename });
return base.Json(new { success = success, recordPath = RecordPath, fileName = file });
}
catch (Exception ex)
{
Log.Error(ex.ToString());
throw;
}
}

conclusion

The above is small make up to introduce the WeChat server download file to the local example code, hope to help you, if you have any questions please give me a message, small make up will reply to you in time. Thank you very much for your support to developpaer!