wei
2021-03-03 d4811b7d34b45ff6b21b97f11da128b5572ec526
HDL_ON/Common/FileUtlis.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using HDL_ON.Entity;
namespace HDL_ON.Common
@@ -30,7 +31,7 @@
            {
                if (string.IsNullOrEmpty(accountPath) || !accountPath.Contains(UserInfo.Current.ID))
                {
                    accountPath = Path.Combine(RootPath, UserInfo.Current.CurReginID);
                    accountPath = Path.Combine(RootPath, UserInfo.Current.ID);
                    if (!Directory.Exists(accountPath))
                    {
                        Directory.CreateDirectory(accountPath);
@@ -47,9 +48,9 @@
        {
            get
            {
                if (string.IsNullOrEmpty(regionPath) || !regionPath.Contains(UserInfo.Current.CurReginID ))
                if (string.IsNullOrEmpty(regionPath) || !regionPath.Contains(DB_ResidenceData.Instance.CurrentRegion.RegionID ))
                {
                    regionPath = Path.Combine(AccountPath, UserInfo.Current.CurReginID);
                    regionPath = Path.Combine(AccountPath, DB_ResidenceData.Instance.CurrentRegion.RegionID);
                    if (!Directory.Exists(regionPath))
                    {
                        Directory.CreateDirectory(regionPath);
@@ -59,7 +60,7 @@
            }
        }
        //public List<>
        /// <summary>
@@ -119,7 +120,7 @@
                fs = new FileStream(Path.Combine(RegionPath, fileName), FileMode.Create, FileAccess.Write);
                fs.Write(bytes, 0, bytes.Length);
                fs.Flush();
                MainPage.Log("SaveFile:" + fileName);
                MainPage.Log($"SaveFile:{fileName}");//Path:{RegionPath}
                return true;
            }
            catch (Exception ex)
@@ -269,5 +270,81 @@
                }
            }
        }
        /// <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();
                        }
                    }
                });
            }
        }
    }
    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;
        }
    }
}