微信服務(wù)器中下載文件到本地的實(shí)例代碼
更新時(shí)間:2018年08月02日 15:26:26 作者:老麟先生
這篇文章主要介紹了微信服務(wù)器中下載文件到本地的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
從微信服務(wù)器中下載文件到本地的實(shí)例代碼,如下所示:
//從微信服務(wù)器中下載文件到本地
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");
//絕對(duì)路徑
string uploadPath = ConfigurationManager.AppSettings["RecordPath"] + DateTime.Now.ToString("yyyyMMdd");
//新的文件名
filename = DateTime.Now.ToString("HHmmssfff_yyyyMMdd") + (new Random()).Next().ToString().Substring(0, 4) + ".amr";
//相對(duì)路徑
file = @"GetImages/Images/" + DateTime.Now.ToString("yyyyMMdd") + @"/" + filename;
savepath = uploadPath + "\\" + filename;
try
{
if (!Directory.Exists(uploadPath))
{
//自動(dòng)生成文件夾
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;
}
}
總結(jié)
以上所述是小編給大家介紹的微信服務(wù)器中下載文件到本地的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
調(diào)試ASP.NET應(yīng)用程序的方法和技巧
調(diào)試ASP.NET應(yīng)用程序的方法和技巧...2006-09-09
ASP.NET Core為Ocelot網(wǎng)關(guān)配置Swagger
這篇文章介紹了ASP.NET Core為Ocelot網(wǎng)關(guān)配置Swagger的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
ASP.Net中英文復(fù)合檢索文本框?qū)崿F(xiàn)思路及代碼
前段時(shí)間,寫一個(gè)用戶部門的管理頁(yè)面,需要對(duì)后臺(tái)獲取的用戶數(shù)據(jù)實(shí)現(xiàn)英漢檢索功能于是就有了下文,編輯的很詳細(xì)圖文并茂呢?感興趣的你可不要錯(cuò)過了哈,或許本文對(duì)你有所幫助呢2013-02-02
詳解免費(fèi)開源的DotNet任務(wù)調(diào)度組件Quartz.NET(.NET組件介紹之五)
本篇文章主要介紹免費(fèi)開源的DotNet任務(wù)調(diào)度組件Quartz.NET(.NET組件介紹之五),具有一定參考價(jià)值,有興趣的可以了解一下。2016-12-12

