UploadFileHelper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Web;
  10. namespace SysBaseLibs
  11. {
  12. public class UploadFileHelper
  13. {
  14. public static UploadFileHelper Instance { get; } = new UploadFileHelper();
  15. public UploadResult UploadFiles(string filePath, string fileType, HttpRequestBase request, HttpServerUtilityBase server)
  16. {
  17. UploadResult result = new UploadResult
  18. {
  19. FileUrls = new ArrayList(),
  20. Success = false,
  21. Msg = "Failure!",
  22. VirtualPathUrls = new ArrayList(),
  23. };
  24. //遍历所有文件域
  25. foreach (string fieldName in request.Files.AllKeys)
  26. {
  27. HttpPostedFileBase file = request.Files.Get(fieldName);
  28. string ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
  29. filePath += ymd + "/";
  30. string fileName = DateTime.Now.ToString("yyyyMMddhhmmss_ffff");
  31. string urlPath = $"{filePath}{fileName}.{fileType}";
  32. string virtualPath = server.MapPath(urlPath);
  33. try
  34. {
  35. if (!Directory.Exists(server.MapPath(filePath)))
  36. Directory.CreateDirectory(server.MapPath(filePath));
  37. file?.SaveAs(virtualPath);
  38. result.FileUrls.Add(urlPath);
  39. result.VirtualPathUrls.Add(virtualPath);
  40. result.Success = true;
  41. result.Msg = "Success!";
  42. }
  43. catch (Exception e)
  44. {
  45. result.Msg = e.Message;
  46. return result;
  47. }
  48. }
  49. return result;
  50. }
  51. public UploadResult UploadFiles2(string filePath, HttpPostedFileBase requestFile, HttpServerUtilityBase server)
  52. {
  53. UploadResult result = new UploadResult
  54. {
  55. FileUrls = new ArrayList(),
  56. Success = false,
  57. Msg = "Failure!"
  58. };
  59. //遍历所有文件域
  60. string urlPath = $"{filePath}";
  61. string virtualPath = server.MapPath(urlPath);
  62. try
  63. {
  64. if (File.Exists(virtualPath))
  65. File.Delete(virtualPath);
  66. requestFile?.SaveAs(virtualPath);
  67. result.FileUrls.Add(urlPath);
  68. result.VirtualPathUrls.Add(virtualPath);
  69. result.Success = true;
  70. result.Msg = "Success!";
  71. }
  72. catch (Exception e)
  73. {
  74. result.Msg = e.Message;
  75. return result;
  76. }
  77. return result;
  78. }
  79. }
  80. public class UploadResult
  81. {
  82. /// <summary>
  83. /// 表示文件是否已上传成功。
  84. /// </summary>
  85. // ReSharper disable once InconsistentNaming
  86. public bool Success;
  87. /// <summary>
  88. /// 自定义的附加消息。
  89. /// </summary>
  90. // ReSharper disable once InconsistentNaming
  91. public string Msg;
  92. /// <summary>
  93. /// 表示所有文件的保存地址,该变量为一个数组。
  94. /// </summary>
  95. // ReSharper disable once InconsistentNaming
  96. public ArrayList FileUrls;
  97. public ArrayList VirtualPathUrls;
  98. }
  99. }