微信服务器中下载文件到本地

2018-09-18 06:54:15来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

//从微信服务器中下载文件到本地

public JsonResult UploadRecord(string ServerId)
{
try
{
member m = base.CurrentUser;
string msg = "成功";
bool success = true;
SiteSettingsInfo setting = base.CurrentSiteSetting;
string file = string.Empty;
string content = string.Empty;
string strpath = string.Empty;
string savepath = string.Empty;

//微信地址
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");
//绝对路径
string uploadPath = ConfigurationManager.AppSettings["RecordPath"] + DateTime.Now.ToString("yyyyMMdd");
//新的文件名
filename = DateTime.Now.ToString("HHmmssfff_yyyyMMdd") + (new Random()).Next().ToString().Substring(0, 4) + ".amr";
//相对路径
file = @"GetImages/Images/" + DateTime.Now.ToString("yyyyMMdd") + @"/" + filename;

savepath = uploadPath + "\\" + filename;
try
{
if (!Directory.Exists(uploadPath))
{
//自动生成文件夹
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;
}
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Easyui datagrid绑定数据,新增,修改,删除方法(一)

下一篇:ASP.NET MVC 使用Unity实现Ioc