FileUpload.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using Abp.Logging;
  2. using SixLabors.ImageSharp;
  3. using VberZero.Tools.StringModel;
  4. namespace VberZero.Tools.FileHelpers;
  5. public static class FileUpload
  6. {
  7. private static string GetFileExt(string fileName)
  8. {
  9. string fileExt = fileName.Substring(fileName.LastIndexOf(".", StringComparison.Ordinal) + 1, fileName.Length - fileName.LastIndexOf(".", StringComparison.Ordinal) - 1);
  10. return fileExt.ToLower();
  11. }
  12. public static string Base64ToFile(this string base64Str, string fileNamePath, IWebHostEnvironment env = null, bool webRoot = true)
  13. {
  14. string lcRetVal = "error@";
  15. try
  16. {
  17. string path = $"{(env == null ? AppDomain.CurrentDomain.BaseDirectory : webRoot ? env.WebRootPath : env.ContentRootPath)}{fileNamePath}";
  18. SaveBase64File(base64Str, path);
  19. lcRetVal = fileNamePath;
  20. }
  21. catch (Exception e)
  22. {
  23. LogHelper.LogException(e);
  24. lcRetVal += "文件上传异常。";
  25. }
  26. return lcRetVal;
  27. }
  28. public static string Base64ToPng(this string base64String, string fileNamePath, IWebHostEnvironment env = null, bool webRoot = true)
  29. {
  30. string lcRetVal = "error@";
  31. try
  32. {
  33. string path = $"{(env == null ? AppDomain.CurrentDomain.BaseDirectory : webRoot ? env.WebRootPath : env.ContentRootPath)}{fileNamePath}";
  34. SaveBase64Png(base64String, path);
  35. lcRetVal = fileNamePath;
  36. }
  37. catch (Exception e)
  38. {
  39. LogHelper.LogException(e);
  40. lcRetVal += "文件上传异常。";
  41. }
  42. return lcRetVal;
  43. }
  44. public static string Base64ToFile(this string base64Str, string fileName, string filePath, string fileExt, IWebHostEnvironment env = null, bool webRoot = true)
  45. {
  46. fileName = $"{fileName}.{fileExt}";
  47. string lcRetVal = Base64ToFile(base64Str, fileName, filePath, env, webRoot);
  48. return lcRetVal;
  49. }
  50. public static string Base64ToFile(this string base64Str, string fileName, string filePath, IWebHostEnvironment env = null, bool webRoot = true)
  51. {
  52. string lcRetVal = "error@";
  53. try
  54. {
  55. filePath = filePath.Sw("\\");
  56. filePath = filePath.Ew("\\");
  57. string path = $"{(env == null ? AppDomain.CurrentDomain.BaseDirectory : webRoot ? env.WebRootPath : env.ContentRootPath)}{filePath}";
  58. if (!Directory.Exists(path))
  59. Directory.CreateDirectory(path);
  60. SaveBase64File(base64Str, path, fileName);
  61. lcRetVal = filePath + fileName;
  62. }
  63. catch (Exception e)
  64. {
  65. LogHelper.LogException(e);
  66. lcRetVal += "文件上传异常。";
  67. }
  68. return lcRetVal;
  69. }
  70. public static string Base64ToPng(this string base64String, string fileName, string filePath, IWebHostEnvironment env = null, bool webRoot = true)
  71. {
  72. string lcRetVal = "error@";
  73. try
  74. {
  75. fileName = $"{fileName}-{DateTime.Now:yyMMddHHmmss}{new Random().Next(1000, 9999)}.png";
  76. filePath = filePath.Sw("\\");
  77. filePath = filePath.Ew("\\");
  78. string path = $"{(env == null ? AppDomain.CurrentDomain.BaseDirectory : webRoot ? env.WebRootPath : env.ContentRootPath)}{filePath}";
  79. SaveBase64Png(base64String, path, fileName);
  80. lcRetVal = filePath + fileName;
  81. }
  82. catch (Exception e)
  83. {
  84. LogHelper.LogException(e);
  85. lcRetVal += "文件上传异常。";
  86. }
  87. return lcRetVal;
  88. }
  89. public static void SaveBase64File(this string base64String, string fileNamePath)
  90. {
  91. if (fileNamePath == null)
  92. {
  93. return;
  94. }
  95. var filePath = fileNamePath.Substring(0, fileNamePath.LastIndexOf('\\'));
  96. if (!Directory.Exists(filePath))
  97. Directory.CreateDirectory(filePath);
  98. byte[] bytes = Convert.FromBase64String(base64String);
  99. using FileStream fs = new FileStream(fileNamePath, FileMode.Create, FileAccess.Write);
  100. fs.Write(bytes, 0, bytes.Length);
  101. fs.Close();
  102. }
  103. public static void SaveBase64Png(this string base64String, string fileNamePath)
  104. {
  105. if (fileNamePath == null)
  106. {
  107. return;
  108. }
  109. var filePath = fileNamePath.Substring(0, fileNamePath.LastIndexOf('\\'));
  110. if (!Directory.Exists(filePath))
  111. Directory.CreateDirectory(filePath);
  112. base64String = base64String.StartsWith("data") ? base64String.Split(',')[1] : base64String;
  113. byte[] bytes = Convert.FromBase64String(base64String);
  114. //using MemoryStream ms = new MemoryStream(bytes);
  115. var img = Image.Load(bytes);
  116. img.SaveAsPng(fileNamePath);
  117. }
  118. public static void SaveBase64File(this string base64String, string fileNamePath, string fileName)
  119. {
  120. if (fileNamePath == null)
  121. {
  122. return;
  123. }
  124. if (!Directory.Exists(fileNamePath))
  125. Directory.CreateDirectory(fileNamePath);
  126. byte[] bytes = Convert.FromBase64String(base64String);
  127. using FileStream fs = new FileStream(Path.Combine(fileNamePath, fileName), FileMode.Create, FileAccess.Write);
  128. fs.Write(bytes, 0, bytes.Length);
  129. fs.Close();
  130. }
  131. public static void SaveBase64Png(this string base64String, string fileNamePath, string fileName)
  132. {
  133. if (fileNamePath == null)
  134. {
  135. return;
  136. }
  137. if (!Directory.Exists(fileNamePath))
  138. Directory.CreateDirectory(fileNamePath);
  139. base64String = base64String.StartsWith("data") ? base64String.Split(',')[1] : base64String;
  140. byte[] bytes = Convert.FromBase64String(base64String);
  141. //using MemoryStream ms = new MemoryStream(bytes);
  142. var img = Image.Load(bytes);
  143. img.SaveAsPng(Path.Combine(fileNamePath, fileName));
  144. }
  145. public static void DeleteFile(this string fileNamePath, IWebHostEnvironment env = null, bool webRoot = true)
  146. {
  147. try
  148. {
  149. fileNamePath = fileNamePath.Sw("\\");
  150. string path = $"{(env == null ? AppDomain.CurrentDomain.BaseDirectory : webRoot ? env.WebRootPath : env.ContentRootPath)}{fileNamePath}";
  151. if (File.Exists(path))
  152. File.Delete(path);
  153. }
  154. catch
  155. {
  156. //
  157. }
  158. }
  159. }