實例如下所示:
1
2
3
4
5
6
7
|
System.Net.WebClient myWebClient = new System.Net.WebClient(); //將頭像保存到服務(wù)器 string virPath = "/Uploads/AppImage/" + user.Id + "/"; CreateDir(virPath); string fileName = Guid.NewGuid().ToString() + ".png"; myWebClient.DownloadFile(headimgurl, System.Web.HttpContext.Current.Request.PhysicalApplicationPath + virPath + fileName); user.Portrait = virPath + fileName; |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#region 創(chuàng)建目錄 /// < summary > /// 創(chuàng)建目錄 /// </ summary > /// < param name = "dir" >要創(chuàng)建的目錄路徑包括目錄名</ param > public static void CreateDir(string dir) { if (dir.Length == 0) return; if (!Directory.Exists(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir)) Directory.CreateDirectory(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + dir) ; } #endregion |
以上這篇C#抓取網(wǎng)絡(luò)圖片保存到本地的實現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/yechangzhong-826217795/p/7822757.html