欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-21 15:32:49
从微信服务器中下载文件到本地的实例代码,如下所示: //从微信服务器中下载文件到本地 public jsonresult uploadrecord(string...

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

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

总结

以上所述是小编给大家介绍的微信服务器中下载文件到本地的实例代码,希望对大家有所帮助