| | |
| | | public class WebUtils |
| | | { |
| | | #region LFData |
| | | private static readonly object _obj = new object(); |
| | | |
| | | private static string _lfData; |
| | | |
| | | public static string LFData |
| | |
| | | } |
| | | |
| | | 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 |
| | |
| | | throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "文件不存在!")); |
| | | } |
| | | |
| | | return DownloadFile(file, "layer.json"); |
| | | return DownloadFile(file, "layer.json", "application/json"); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | 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"); |
| | |
| | | throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "文件不存在!")); |
| | | } |
| | | |
| | | return DownloadFile(file, y + ".png"); |
| | | return DownloadFile(file, y + ".png", "image/png"); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | 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); |
| | |
| | | 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)); |
| | |
| | | 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> |