From 43b0d5870d528f23ecd6aeceb6cfd4325188b46f Mon Sep 17 00:00:00 2001 From: wxr <464027401@qq.com> Date: 星期四, 01 七月 2021 15:50:43 +0800 Subject: [PATCH] Revert "1" --- HDL_ON/Common/FileUtlis.cs | 132 ++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 126 insertions(+), 6 deletions(-) diff --git a/HDL_ON/Common/FileUtlis.cs b/HDL_ON/Common/FileUtlis.cs index 03d1695..d0a8977 100644 --- a/HDL_ON/Common/FileUtlis.cs +++ b/HDL_ON/Common/FileUtlis.cs @@ -1,11 +1,12 @@ 锘縰sing System; using System.Collections.Generic; using System.IO; +using System.Net; using HDL_ON.Entity; namespace HDL_ON.Common { - public class FileUtlis + public class FileUtlis { static Common.FileUtlis _temp; public static Common.FileUtlis Files @@ -25,7 +26,8 @@ string RootPath = Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "/"; string accountPath; - string AccountPath { + string AccountPath + { get { if (string.IsNullOrEmpty(accountPath) || !accountPath.Contains(UserInfo.Current.ID)) @@ -47,9 +49,13 @@ { get { - if (string.IsNullOrEmpty(regionPath) || !regionPath.Contains(DB_ResidenceData.Instance.CurrentRegion.RegionID )) + if (DB_ResidenceData.Instance.CurrentRegion == null || DB_ResidenceData.Instance.CurrentRegion.id == null) { - regionPath = Path.Combine(AccountPath, DB_ResidenceData.Instance.CurrentRegion.RegionID); + return AccountPath; + } + if (string.IsNullOrEmpty(regionPath) || !regionPath.Contains(DB_ResidenceData.Instance.CurrentRegion.id)) + { + regionPath = Path.Combine(AccountPath, DB_ResidenceData.Instance.CurrentRegion.id); if (!Directory.Exists(regionPath)) { Directory.CreateDirectory(regionPath); @@ -227,8 +233,6 @@ } } - - public byte[] ReadFile(string fileName) { FileStream fs = null; @@ -270,6 +274,122 @@ } } + // 璇诲彇鎸囧畾璺緞鏂囦欢鍐呭 + public byte[] ReadFileForPath(string path) + { + FileStream fs = null; + try + { + if (File.Exists(path)) + { + fs = new FileStream(path, FileMode.Open, FileAccess.Read); + } + else + { + return new byte[0]; + } + byte[] bytes = new byte[fs.Length]; + fs.Read(bytes, 0, bytes.Length); + return bytes; + } + catch + { + return new byte[0]; + } + finally + { + try + { + if (fs != null) + { + fs.Close(); + } + } + catch + { + + } + } + } + + /// <summary> + /// 涓嬭浇缃戠粶鍥剧墖 + /// </summary> + /// <param name="fileName"></param> + /// <param name="url"></param> + public void DownLoadImage(string fileName, string url, Action action) + { + if (!File.Exists(fileName)) + { + System.Threading.Tasks.Task.Run(() => + { + FileStream fs = null; + try + { + MyWebClient webClient = new MyWebClient(); + byte[] recevieBytes = webClient.DownloadData(new Uri(url)); + fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Create, FileAccess.Write); + //fs = new FileStream(fileName, FileMode.Create, FileAccess.Write); + fs.Write(recevieBytes, 0, recevieBytes.Length); + fs.Flush(); + } + catch (Exception ex) + { + MainPage.Log($"down image : {ex.Message}"); + } + finally + { + if (fs != null) + { + fs.Close(); + action?.Invoke(); + } + } + }); + } + else + { + action?.Invoke(); + } + } } + + + public class MyWebClient : WebClient + { + int _timeout = 10000; + + /// <summary> + /// 瓒呮椂鏃堕棿(姣) + /// </summary> + public int Timeout + { + get + { + return _timeout; + } + set + { + _timeout = value; + } + } + public MyWebClient() + { + + } + + public MyWebClient(int timeout) + { + this._timeout = timeout; + } + + protected override WebRequest GetWebRequest(Uri address) + { + var result = base.GetWebRequest(address); + result.Timeout = this._timeout; + return result; + } + } + } -- Gitblit v1.8.0