C#抓取網絡圖片保存到本地
System.Net.WebClient myWebClient = new System.Net.WebClient(); //將頭像保存到服務器 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;
#region 創建目錄 /// <summary> /// 創建目錄 /// </summary> /// <param name="dir">要創建的目錄路徑包括目錄名</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