FileHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using WMS.Util;
  8. namespace WMS.BZUtil
  9. {
  10. public static class FileHelper
  11. {
  12. /// <summary>
  13. /// 读取根目录下面的文件
  14. /// </summary>
  15. /// <param name="filePath">文件路径</param>
  16. /// <returns></returns>
  17. public static byte[] ReadRoot(string filePath)
  18. {
  19. string rootPath = ConfigHelper.GetValue<string>("baseDir") + "/wwwroot";
  20. string path = rootPath + filePath;
  21. return File.ReadAllBytes(path);
  22. }
  23. /// <summary>
  24. /// 读取缓存文件
  25. /// </summary>
  26. /// <param name="fileName">文件路径</param>
  27. /// <returns></returns>
  28. public static byte[] ReadCache(string fileName)
  29. {
  30. string rootPath = ConfigHelper.GetValue<string>("baseDir") + "/wwwroot/cache/";
  31. string path = rootPath + fileName;
  32. return File.ReadAllBytes(path);
  33. }
  34. /// <summary>
  35. /// 读取缓存文件
  36. /// </summary>
  37. /// <param name="fileName">文件路径</param>
  38. /// <param name="buffer">文件数据</param>
  39. /// <returns></returns>
  40. public static void WriteCache(string fileName, byte[] buffer)
  41. {
  42. string rootPath = ConfigHelper.GetValue<string>("baseDir") + "/wwwroot/cache/";
  43. if (!Directory.Exists(rootPath))
  44. {
  45. Directory.CreateDirectory(rootPath);
  46. }
  47. string path = rootPath + fileName;
  48. FileInfo file = new FileInfo(path);
  49. FileStream fs = file.Create();
  50. fs.Write(buffer, 0, buffer.Length);
  51. fs.Close();
  52. }
  53. /// <summary>
  54. /// 读取缓存文件
  55. /// </summary>
  56. /// <param name="fileName">文件路径</param>
  57. /// <returns></returns>
  58. public static void RemoveCache(string fileName)
  59. {
  60. string rootPath = ConfigHelper.GetValue<string>("baseDir") + "/wwwroot/cache/";
  61. string path = rootPath + fileName;
  62. File.Delete(path);
  63. }
  64. /// <summary>
  65. /// 读取文件
  66. /// </summary>
  67. /// <param name="filePath">文件绝对路径</param>
  68. /// <returns></returns>
  69. public static byte[] Read(string filePath)
  70. {
  71. return File.ReadAllBytes(filePath);
  72. }
  73. /// <summary>
  74. /// 获取文件的传输类型
  75. /// </summary>
  76. /// <param name="fileExt">文件扩展名</param>
  77. /// <returns></returns>
  78. public static string getContentType(string fileExt)
  79. {
  80. string contentType = "";
  81. switch (fileExt?.ToLower())
  82. {
  83. case "jpg":
  84. case "jpeg":
  85. case "gif":
  86. case "png":
  87. case "webp":
  88. contentType = "image/" + fileExt.ToLower();
  89. break;
  90. case "bmp":
  91. contentType = "application/x-bmp";
  92. break;
  93. case "pdf":
  94. contentType = "application/" + fileExt.ToLower();
  95. break;
  96. case "txt":
  97. contentType = "text/plain";
  98. break;
  99. case "csv":
  100. contentType = "";
  101. break;
  102. case "html":
  103. contentType = "text/html";
  104. break;
  105. default:
  106. contentType = "application/octet-stream";
  107. break;
  108. }
  109. return contentType;
  110. }
  111. }
  112. }