using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WMS.Util; namespace WMS.BZUtil { public static class FileHelper { /// /// 读取根目录下面的文件 /// /// 文件路径 /// public static byte[] ReadRoot(string filePath) { string rootPath = ConfigHelper.GetValue("baseDir") + "/wwwroot"; string path = rootPath + filePath; return File.ReadAllBytes(path); } /// /// 读取缓存文件 /// /// 文件路径 /// public static byte[] ReadCache(string fileName) { string rootPath = ConfigHelper.GetValue("baseDir") + "/wwwroot/cache/"; string path = rootPath + fileName; return File.ReadAllBytes(path); } /// /// 读取缓存文件 /// /// 文件路径 /// 文件数据 /// public static void WriteCache(string fileName, byte[] buffer) { string rootPath = ConfigHelper.GetValue("baseDir") + "/wwwroot/cache/"; if (!Directory.Exists(rootPath)) { Directory.CreateDirectory(rootPath); } string path = rootPath + fileName; FileInfo file = new FileInfo(path); FileStream fs = file.Create(); fs.Write(buffer, 0, buffer.Length); fs.Close(); } /// /// 读取缓存文件 /// /// 文件路径 /// public static void RemoveCache(string fileName) { string rootPath = ConfigHelper.GetValue("baseDir") + "/wwwroot/cache/"; string path = rootPath + fileName; File.Delete(path); } /// /// 读取文件 /// /// 文件绝对路径 /// public static byte[] Read(string filePath) { return File.ReadAllBytes(filePath); } /// /// 获取文件的传输类型 /// /// 文件扩展名 /// public static string getContentType(string fileExt) { string contentType = ""; switch (fileExt?.ToLower()) { case "jpg": case "jpeg": case "gif": case "png": case "webp": contentType = "image/" + fileExt.ToLower(); break; case "bmp": contentType = "application/x-bmp"; break; case "pdf": contentType = "application/" + fileExt.ToLower(); break; case "txt": contentType = "text/plain"; break; case "csv": contentType = ""; break; case "html": contentType = "text/html"; break; default: contentType = "application/octet-stream"; break; } return contentType; } } }