JsCssUtil.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Text;
  5. using Yahoo.Yui.Compressor;
  6. namespace WMS.Util
  7. {
  8. /// <summary>
  9. /// 描 述:js,css,文件压缩和下载
  10. /// </summary>
  11. public class JsCssUtil
  12. {
  13. private static JavaScriptCompressor javaScriptCompressor = new JavaScriptCompressor();
  14. private static CssCompressor cssCompressor = new CssCompressor();
  15. /// <summary>
  16. /// 读取js文件内容并压缩
  17. /// </summary>
  18. /// <param name="filePathlist"></param>
  19. /// <returns></returns>
  20. public static string ReadJSFile(string[] filePathlist, bool IsJsCompressor)
  21. {
  22. StringBuilder jsStr = new StringBuilder();
  23. try
  24. {
  25. string rootPath = ConfigHelper.GetValue<string>("baseDir");//+ "/wwwroot";
  26. foreach (var filePath in filePathlist)
  27. {
  28. string path = rootPath + filePath;
  29. if (File.Exists(path))
  30. {
  31. string content = File.ReadAllText(path, Encoding.UTF8);
  32. if (IsJsCompressor)
  33. {
  34. content = javaScriptCompressor.Compress(content);
  35. }
  36. jsStr.Append(content);
  37. }
  38. }
  39. return jsStr.ToString();
  40. }
  41. catch
  42. {
  43. return "";
  44. }
  45. }
  46. /// <summary>
  47. /// 读取css 文件内容并压缩
  48. /// </summary>
  49. /// <param name="filePathlist"></param>
  50. /// <returns></returns>
  51. public static string ReadCssFile(string[] filePathlist, bool IsCssCompressor)
  52. {
  53. StringBuilder cssStr = new StringBuilder();
  54. try
  55. {
  56. string rootPath = ConfigHelper.GetValue<string>("baseDir");//+ "/wwwroot";
  57. foreach (var filePath in filePathlist)
  58. {
  59. string path = rootPath + filePath;
  60. if (File.Exists(path))
  61. {
  62. string content = File.ReadAllText(path, Encoding.UTF8);
  63. if (IsCssCompressor)
  64. {
  65. content = cssCompressor.Compress(content);
  66. }
  67. cssStr.Append(content);
  68. }
  69. }
  70. return cssStr.ToString();
  71. }
  72. catch
  73. {
  74. return cssStr.ToString();
  75. }
  76. }
  77. }
  78. }