微信服务器中下载文件到本地的实例代码

所属分类: 网络编程 / ASP.NET 阅读数: 1084
收藏 0 赞 0 分享

从微信服务器中下载文件到本地的实例代码,如下所示:

//从微信服务器中下载文件到本地
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;
}
}

以上所述是小编给大家介绍的微信服务器中下载文件到本地的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

asp.net网站首页根据IP自动跳转指定页面的示例

本文介绍的程序主要实现根据IP地址或地址段或IP所在城市进行自动跳转到指定页面的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

asp.net无法获取iis目录的问题解决方法

本文介绍了Asp.Net无法获取IIS拾取目录的解决办法,需要的朋友可以参考下
收藏 0 赞 0 分享

.net中 发送邮件内容嵌入图片的具体实例

这篇文章主要介绍了.net中 发送邮件内容嵌入图片的具体实例,需要的朋友可以参考下
收藏 0 赞 0 分享

在.net中用CheckBoxList实现单选

用CheckBoxList实现单选的原因是我觉得CheckBoxList控件页面展示效果要好看一些,需要的朋友可以参考下
收藏 0 赞 0 分享

ASP.NET―001:GridView绑定List、页面返回值具体实现

这篇文章主要介绍了ASP.NET―GridView绑定List、页面返回值具体实现,需要的朋友可以参考下
收藏 0 赞 0 分享

Ajax实现异步刷新验证用户名是否已存在的具体方法

由于要做一个注册页面,看到许多网站上都是使用Ajax异步刷新验证用户名是否可用的,所以自己也动手做一个小实例
收藏 0 赞 0 分享

ASP.NET汉字转拼音 - 输入汉字获取其拼音的具体实现

这篇文章主要介绍了ASP.NET汉字转拼音 - 输入汉字获取其拼音的具体实现,需要的朋友可以参考下
收藏 0 赞 0 分享

.Net消息队列的使用方法

这篇文章主要介绍了.Net消息队列的使用方法,需要的朋友可以参考下
收藏 0 赞 0 分享

C# web api返回类型设置为json的两种方法

web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面为大家介绍几种不错的方法
收藏 0 赞 0 分享

asp.net获取网站目录物理路径示例

这篇文章主要介绍了asp.net获取网站目录物理路径的方法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多