FileService.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Http;
  4. using AI.Platform.Service.Common;
  5. using AI.Platform.Core.Entity;
  6. using Microsoft.AspNetCore.Components;
  7. using AI.Platform.Core;
  8. using static AI.Platform.Core.Entity.PublicEnum;
  9. using System.Net;
  10. using System;
  11. using AI.Platform.Service.Output;
  12. using Microsoft.AspNetCore.Http.HttpResults;
  13. namespace AI.Platform.Service;
  14. [ApiGroup(ApiGroupNames.System)]
  15. public class FileService : BaseService
  16. {
  17. /// <summary>
  18. ///
  19. /// </summary>
  20. private readonly IHttpContextAccessor _contextAccessor;
  21. public FileService(IHttpContextAccessor contextAccessor)
  22. {
  23. _contextAccessor = contextAccessor;
  24. }
  25. /// <summary>
  26. /// 上传图片
  27. /// </summary>
  28. /// <param name="classification"></param>
  29. /// <returns></returns>
  30. [HttpPost, AllowAnonymous]
  31. public async Task<object> UploadImage(string classification = "default")
  32. {
  33. var host = _contextAccessor.GetHost();
  34. var file = _contextAccessor.HttpContext.Request.Form.Files[0];
  35. if (file.Length <= 0)
  36. {
  37. return "";
  38. }
  39. using var memoryStream = new MemoryStream();
  40. file.CopyTo(memoryStream);
  41. var fileBytes = memoryStream.ToArray();
  42. // 获取文件扩展名
  43. var fileExtension = Path.GetExtension(file.FileName);
  44. // 生成文件名
  45. var id = Guid.NewGuid().ToString().ToUpper().Replace("-", "");
  46. var fileName = $"{id}{fileExtension}";
  47. // 获取当前日期
  48. var currentDate = DateTime.Now;
  49. // 构建目录路径
  50. var directoryPath = Path.Combine("wwwroot", "image", classification, currentDate.ToString("yyyy"), currentDate.ToString("MM"), currentDate.ToString("dd"));
  51. // 创建目录
  52. if (!Directory.Exists(directoryPath))
  53. {
  54. Directory.CreateDirectory(directoryPath);
  55. }
  56. // 构建文件路径
  57. var filePath = Path.Combine(directoryPath, fileName);
  58. // 保存文件
  59. await System.IO.File.WriteAllBytesAsync(filePath, fileBytes);
  60. // 构建返回的相对路径
  61. var relativePath = $"/image/{classification}/{currentDate:yyyy}/{currentDate:MM}/{currentDate:dd}/{fileName}";
  62. return relativePath;
  63. }
  64. /// <summary>
  65. /// 上传多媒体文件
  66. /// </summary>
  67. /// <param name="classification"></param>
  68. /// <returns></returns>
  69. [HttpPost, AllowAnonymous]
  70. public async Task<MediaFileUploadOutput> UploadMedia(IFormFile file)
  71. {
  72. if (file == null || file.Length <= 0)
  73. {
  74. return new MediaFileUploadOutput()
  75. {
  76. isSuccess = false,
  77. msg = "上传文件为空"
  78. };
  79. }
  80. using var memoryStream = new MemoryStream();
  81. file.CopyTo(memoryStream);
  82. var fileBytes = memoryStream.ToArray();
  83. // 获取文件扩展名
  84. var fileExtension = Path.GetExtension(file.FileName);
  85. // 生成文件名
  86. var id = Guid.NewGuid().ToString().ToUpper().Replace("-", "");
  87. var fileName = $"{id}_{file.FileName}";
  88. // 获取当前日期
  89. var currentDate = DateTime.Now;
  90. // 构建目录路径
  91. var directoryPath = Path.Combine("wwwroot", "madia", currentDate.ToString("MM"), currentDate.ToString("dd"));
  92. // 创建目录
  93. if (!Directory.Exists(directoryPath))
  94. {
  95. Directory.CreateDirectory(directoryPath);
  96. }
  97. // 构建文件路径
  98. var filePath = Path.Combine(directoryPath, fileName);
  99. // 保存文件
  100. await System.IO.File.WriteAllBytesAsync(filePath, fileBytes);
  101. //// 构建返回的相对路径
  102. //var relativePath = $"/image/{currentDate:yyyy}/{currentDate:MM}/{currentDate:dd}/{fileName}";
  103. //return relativePath;
  104. return new MediaFileUploadOutput()
  105. {
  106. isSuccess = true,
  107. msg = "上传成功",
  108. fileName = file.FileName,
  109. guidName = fileName,
  110. savePath = filePath,
  111. extension = fileExtension
  112. };
  113. }
  114. }