From 56e9a081df00c802d17b53b83cbe779015bdcf38 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 18 十月 2023 16:48:19 +0800
Subject: [PATCH] 1

---
 ExportMap/cs/WebUtils.cs |   98 +++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 90 insertions(+), 8 deletions(-)

diff --git a/ExportMap/cs/WebUtils.cs b/ExportMap/cs/WebUtils.cs
index ed2a2b8..8057ebf 100644
--- a/ExportMap/cs/WebUtils.cs
+++ b/ExportMap/cs/WebUtils.cs
@@ -14,6 +14,8 @@
     public class WebUtils
     {
         #region LFData
+        private static readonly object _obj = new object();
+
         private static string _lfData;
 
         public static string LFData
@@ -26,6 +28,50 @@
                 }
 
                 return _lfData;
+            }
+        }
+
+        private static byte[] _emptyPng;
+
+        public static byte[] EmptyPng
+        {
+            get
+            {
+                if (null == _emptyPng)
+                {
+                    lock (_obj)
+                    {
+                        if (null == _emptyPng)
+                        {
+                            string file = Tools.BaseDir + "Sources\\empty.png";
+                            _emptyPng = ReadFileToBytes(file);
+                        }
+                    }
+                }
+
+                return _emptyPng;
+            }
+        }
+
+        private static byte[] _emptyTerrain;
+
+        public static byte[] EmptyTerrain
+        {
+            get
+            {
+                if (null == _emptyTerrain)
+                {
+                    lock (_obj)
+                    {
+                        if (null == _emptyTerrain)
+                        {
+                            string file = Tools.BaseDir + "Sources\\empty.terrain";
+                            _emptyTerrain = ReadFileToBytes(file);
+                        }
+                    }
+                }
+
+                return _emptyTerrain;
             }
         }
         #endregion
@@ -47,7 +93,7 @@
                 throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "鏂囦欢涓嶅瓨鍦紒"));
             }
 
-            return DownloadFile(file, "layer.json");
+            return DownloadFile(file, "layer.json", "application/json");
         }
 
         /// <summary>
@@ -84,7 +130,7 @@
             string file = basePath + "\\" + z + "\\" + x + "\\" + y + ".terrain";
             if (!File.Exists(file))
             {
-                return DownloadFile(Tools.BaseDir + "\\Sources\\empty.terrain", y + ".terrain");
+                return DownloadFile(EmptyTerrain, y + ".terrain");
             }
 
             return DownloadFile(file, y + ".terrain");
@@ -107,7 +153,7 @@
                 throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "鏂囦欢涓嶅瓨鍦紒"));
             }
 
-            return DownloadFile(file, y + ".png");
+            return DownloadFile(file, y + ".png", "image/png");
         }
 
         /// <summary>
@@ -124,16 +170,16 @@
             string file = basePath + "\\" + z + "\\" + x + "\\" + y + ".png";
             if (!File.Exists(file))
             {
-                return DownloadFile(Tools.BaseDir + "\\Sources\\empty.png", y + ".png");
+                return DownloadFile(EmptyPng, y + ".png", "image/png");
             }
 
-            return DownloadFile(file, y + ".png");
+            return DownloadFile(file, y + ".png", "image/png");
         }
 
         /// <summary>
         /// 涓嬭浇鏂囦欢
         /// </summary>
-        public static HttpResponseMessage DownloadFile(string file, string fileName)
+        public static HttpResponseMessage DownloadFile(string file, string fileName, string mediaType = "application/octet-stream")
         {
             //string fileName = Path.GetFileName(file);
             //FileStream stream = new FileStream(file, FileMode.Open);
@@ -143,7 +189,7 @@
             response.Content = new StreamContent(stream);
             response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
             response.Content.Headers.ContentDisposition.FileName = HttpUtility.UrlEncode(fileName);
-            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
+            response.Content.Headers.ContentType = new MediaTypeHeaderValue(mediaType);
 
             //response.Content.Headers.Add("Cache-Control", "max-age=2592000"); // 缂撳瓨1涓湀
             response.Content.Headers.Expires = DateTimeOffset.Now.AddMonths(1); // new DateTimeOffset(DateTime.Now.AddMonths(1));
@@ -151,7 +197,43 @@
             return response;
         }
 
-        #region 鍙戦�佽姹�
+        /// <summary>
+        /// 涓嬭浇鏂囦欢
+        /// </summary>
+        public static HttpResponseMessage DownloadFile(byte[] bytes, string fileName, string mediaType = "application/octet-stream")
+        {
+            MemoryStream ms = new MemoryStream(bytes);
+
+            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
+            response.Content = new StreamContent(ms);
+            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
+            response.Content.Headers.ContentDisposition.FileName = HttpUtility.UrlEncode(fileName);
+            response.Content.Headers.ContentType = new MediaTypeHeaderValue(mediaType);
+
+            //response.Content.Headers.Add("Cache-Control", "max-age=2592000"); // 缂撳瓨1涓湀
+            response.Content.Headers.Expires = DateTimeOffset.Now.AddMonths(1); // new DateTimeOffset(DateTime.Now.AddMonths(1));
+
+            return response;
+        }
+
+        #region 宸ュ叿绫�
+        /// <summary>
+        /// 璇诲彇鏂囦欢鑷砨yte[]
+        /// </summary>
+        public static byte[] ReadFileToBytes(string file)
+        {
+            using (FileStream fs = new FileStream(file, FileMode.Open))
+            {
+                int len = (int)fs.Length;
+                byte[] bytes = new byte[len];
+
+                fs.Read(bytes, 0, len);
+                fs.Flush();
+
+                return bytes;
+            }
+        }
+
         /// <summary>
         /// Get璇锋眰
         /// </summary>

--
Gitblit v1.9.3