FileController.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using log4net;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Logging;
  5. using NPOI.SS.Formula.Functions;
  6. using Ropin.Inspection.Api.Common;
  7. using Ropin.Inspection.Common.Accessor.Interface;
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Threading.Tasks;
  14. namespace Ropin.Inspection.Api.Controllers
  15. {
  16. public class FileController : BaseController
  17. {
  18. private static readonly ILog log = LogManager.GetLogger(typeof(FileController));
  19. private readonly ILogger<FileController> _logger;
  20. private readonly IClaimsAccessor _claims;
  21. /// <summary>
  22. /// 构造函数
  23. /// </summary>
  24. /// <param name="logger"></param>
  25. /// <param name="claims"></param>
  26. public FileController(ILogger<FileController> logger, IClaimsAccessor claims)
  27. {
  28. _logger = logger;
  29. _claims = claims;
  30. }
  31. /// <summary>
  32. /// 上传文件
  33. /// </summary>
  34. /// <param name="Files"></param>
  35. /// <returns></returns>
  36. [HttpPost("UploadFileAsync")]
  37. public async Task<ApiResult> UploadFileAsync(IFormCollection Files)
  38. {
  39. log.Info($"[开始] 上传文件信息.【{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff")}】");
  40. //_logger.LogInformation($"[开始] 上传文件信息.【{DateTime.Now}】");
  41. try
  42. {
  43. //var form = Request.Form;//直接从表单里面获取文件名不需要参数
  44. string dd = Files["File"];
  45. var form = Files;//定义接收类型的参数
  46. IList<Hashtable> fileResultList = new List<Hashtable>();
  47. IFormFileCollection cols = Request.Form.Files;
  48. if (cols == null || cols.Count == 0)
  49. {
  50. return new ApiResult(ReturnCode.GeneralError, "没有上传文件");
  51. }
  52. foreach (IFormFile file in cols)
  53. {
  54. //定义文件数组后缀格式
  55. string[] LimitFileType = { ".XLS", ".XLSX" , ".DOC", ".DOCX" , ".RTF" , ".AVI", ".RM", ".MP4", ".RMVB", ".WMV", ".MTV", ".MOV", ".MPG", ".MPEG", ".JPG", ".JPEG", ".GIF", ".PNG", ".BMP", ".PDF", ".GLB" };
  56. string[] LimitExcelFileType = { ".XLS", ".XLSX"};
  57. string[] LimitWordFileType = {".DOC", ".DOCX", ".RTF" };
  58. string[] LimitVideoFileType = { ".AVI", ".RM", ".MP4", ".RMVB", ".WMV", ".MTV", ".MOV", ".MPG", ".MPEG" };
  59. string[] LimitPicFileType = { ".JPG", ".JPEG", ".GIF", ".PNG", ".BMP" };
  60. string[] LimitPdfFileType = { ".PDF"};
  61. string[] Limit3DFileType = { ".GLB" };
  62. //LimitFileType.Union(LimitExcelFileType).ToArray().Union(LimitWordFileType).ToArray().Union(LimitVideoFileType).ToArray().Union(LimitPicFileType).ToArray();
  63. //获取文件后缀是否存在数组中
  64. string currentFileExtension = Path.GetExtension(file.FileName).ToUpper();
  65. if (LimitFileType.Contains(currentFileExtension))
  66. {
  67. //为了查看文件就不在重新生成文件名称了
  68. var dateDirectory = DateTime.Now.ToString("yyyyMM");
  69. var relativePath = Path.Combine("wwwroot/uploads/Files/", _claims.Linsence + "/" + dateDirectory);
  70. var directoryPath = Path.Combine(Directory.GetCurrentDirectory(), relativePath);
  71. var fileRelativePath = Path.Combine(relativePath + "/", file.FileName);
  72. if (!Directory.Exists(directoryPath))
  73. {
  74. Directory.CreateDirectory(directoryPath);
  75. }
  76. var path = Path.Combine(directoryPath + "/", file.FileName);
  77. using (var stream = new FileStream(path, FileMode.Create))
  78. {
  79. await Task.Run(()=>{
  80. //再把文件保存的文件夹中
  81. file.CopyTo(stream);
  82. Hashtable hash = new Hashtable();
  83. hash.Add("file", "/" + fileRelativePath);
  84. if (LimitExcelFileType.Contains(currentFileExtension))
  85. {
  86. hash.Add("typeValue", "1");
  87. }
  88. if (LimitWordFileType.Contains(currentFileExtension))
  89. {
  90. hash.Add("typeValue", "2");
  91. }
  92. if (LimitVideoFileType.Contains(currentFileExtension))
  93. {
  94. hash.Add("typeValue", "4");
  95. }
  96. if (LimitPicFileType.Contains(currentFileExtension))
  97. {
  98. hash.Add("typeValue", "3");
  99. }
  100. if (LimitPdfFileType.Contains(currentFileExtension))
  101. {
  102. hash.Add("typeValue", "5");
  103. }
  104. if (Limit3DFileType.Contains(currentFileExtension))
  105. {
  106. hash.Add("typeValue", "6");
  107. }
  108. hash.Add("name", file.Name);
  109. fileResultList.Add(hash);
  110. });
  111. }
  112. }
  113. else
  114. {
  115. return new ApiResult(ReturnCode.GeneralError, "请上传指定格式的文件");
  116. }
  117. }
  118. log.Info($"[结束] 上传文件信息.【{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff")}】");
  119. return new ApiResult<IList<Hashtable>>(fileResultList);
  120. }
  121. catch (Exception ex)
  122. {
  123. log.Info($"[异常] 上传文件信息.【{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff")}】");
  124. return new ApiResult(ReturnCode.GeneralError, ex.Message + ex.InnerException);
  125. }
  126. }
  127. }
  128. }