From 0347d1332ee6490963fd2e0771f2cfcda7bcafe9 Mon Sep 17 00:00:00 2001 From: JLChen <551775569@qq.com> Date: 星期五, 11 十二月 2020 16:40:10 +0800 Subject: [PATCH] 2020-12-11 1.修改图片上传和下载方法。 --- HDL_ON/Common/ImageUtlis.cs | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 110 insertions(+), 8 deletions(-) diff --git a/HDL_ON/Common/ImageUtlis.cs b/HDL_ON/Common/ImageUtlis.cs index c85b61e..83e55bd 100644 --- a/HDL_ON/Common/ImageUtlis.cs +++ b/HDL_ON/Common/ImageUtlis.cs @@ -1,6 +1,7 @@ 锘縰sing System; using Shared; using HDL_ON.DAL.Server; +using System.IO; namespace HDL_ON { @@ -34,6 +35,107 @@ return m_Current; } } + #endregion + + + #region 鏂囦欢璇诲啓 + /// <summary> + /// 鏍圭洰褰� + /// </summary> + string RootPath = Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "/"; + /// <summary> + /// 鏂囦欢鏄惁瀛樺湪 + /// </summary> + /// <param name="filePath">File path.</param> + public bool Exists(string filePath) + { + return File.Exists(Path.Combine(RootPath, filePath)); + } + + /// <summary> + /// Writes the file by bytes. + /// </summary> + /// <returns><c>true</c>, if file by bytes was writed, <c>false</c> otherwise.</returns> + /// <param name="fileName">File name.</param> + /// <param name="bytes">Bytes.</param> + public bool WriteFileByBytes(string fileName, byte[] bytes) + { + if (fileName == null || (fileName = fileName.Trim()) == ("")) + { + return false; + } + + FileStream fs = null; + + try + { + fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Create, FileAccess.Write); + fs.Write(bytes, 0, bytes.Length); + fs.Flush(); + MainPage.Log("SaveFile:" + fileName); + return true; + } + catch (Exception ex) + { + MainPage.Log("FileUtiles Code 113:" + ex.ToString()); + return false; + } + finally + { + try + { + if (fs != null) + { + fs.Close(); + } + } + catch (Exception ex) + { + MainPage.Log("FileUtils Code 121 :" + ex.ToString()); + } + } + } + public byte[] ReadFile(string fileName) + { + FileStream fs = null; + try + { + if (File.Exists(Path.Combine(RootPath, fileName))) + { + fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Open, FileAccess.Read); + } + else if (File.Exists(fileName)) + { + fs = new FileStream(fileName, 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 + { + + } + } + } + #endregion #region 鎴块棿銆佸満鏅� 鍥剧墖涓婁紶鍜屼笅杞� @@ -107,12 +209,12 @@ else { //2.鑷畾涔夊浘鐗囷紝妫�娴嬫槸鍚︽湰鍦扮紦瀛樿繃锛屾病鏈夊垯浜戠涓嬭浇鐒跺悗缂撳瓨 - if (!ignoreCache && Common.FileUtlis.Files.Exists(imageKey)) + if (!ignoreCache && Exists(imageKey)) { //2.1 鏈湴鏈夌紦瀛樺苟涓斾笉蹇界暐缂撳瓨锛岀洿鎺ユ湰鍦拌鍙� if (imageView != null) { - imageView.ImageBytes = Common.FileUtlis.Files.ReadFile(imageKey); + imageView.ImageBytes = ReadFile(imageKey); //Utlis.WriteLine("imageKey 鍔犺浇鑷畾涔夌紦瀛樺浘鐗囨垚鍔�"); } } @@ -124,7 +226,7 @@ byte[] imageBytes = GetImageDownloadUrl(imageKey); if (imageBytes != null) { - Common.FileUtlis.Files.WriteFileByBytes(imageKey, imageBytes); + WriteFileByBytes(imageKey, imageBytes); //2.3 涓嬭浇鎴愬姛锛屾樉绀哄苟缂撳瓨鏈湴 Application.RunOnMainThread(() => { @@ -178,7 +280,7 @@ else { //2.鑷畾涔夊浘鐗囷紝妫�娴嬫槸鍚︽湰鍦扮紦瀛樿繃锛屾病鏈夊垯浜戠涓嬭浇鐒跺悗缂撳瓨 - if (!ignoreCache && Common.FileUtlis.Files.Exists(imageKey)) + if (!ignoreCache && Exists(imageKey)) { //2.1 鏈湴鏈夌紦瀛樺苟涓斾笉蹇界暐缂撳瓨锛岀洿鎺ユ湰鍦拌鍙� if (frameLayout != null) @@ -195,7 +297,7 @@ byte[] imageBytes = GetImageDownloadUrl(imageKey); if (imageBytes != null) { - Common.FileUtlis.Files.WriteFileByBytes(imageKey, imageBytes); + WriteFileByBytes(imageKey, imageBytes); //2.3 涓嬭浇鎴愬姛锛屾樉绀哄苟缂撳瓨鏈湴 Application.RunOnMainThread(() => { @@ -259,10 +361,10 @@ { var imageKey = GetHeadImageKey(userId); byte[] imageBytes; - if (!ignoreCache && Common.FileUtlis.Files.Exists(imageKey)) + if (!ignoreCache && Exists(imageKey)) { //鏈湴鏈夌紦瀛� - imageBytes = Common.FileUtlis.Files.ReadFile(imageKey); + imageBytes = ReadFile(imageKey); } else { @@ -271,7 +373,7 @@ if(imageBytes != null) { //淇濆瓨鍒版湰鍦� - Common.FileUtlis.Files.WriteFileByBytes(imageKey, imageBytes); + WriteFileByBytes(imageKey, imageBytes); } } -- Gitblit v1.8.0