您好,登錄后才能下訂單哦!
微信服務器中怎么下載文件到本地,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
如下所示:
//從微信服務器中下載文件到本地 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; } }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。