管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2023-10-18 56e9a081df00c802d17b53b83cbe779015bdcf38
1
已添加1个文件
已修改1个文件
98 ■■■■■ 文件已修改
ExportMap/Sources/empty.png 补丁 | 查看 | 原始文档 | blame | 历史
ExportMap/cs/WebUtils.cs 98 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ExportMap/Sources/empty.png
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>
        /// 读取文件至byte[]
        /// </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>