ImageController.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Logging;
  4. using Ropin.Inspection.Api.Common;
  5. using Ropin.Inspection.Common.Accessor.Interface;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. namespace Ropin.Inspection.Api.Controllers
  13. {
  14. public class ImageController : BaseController
  15. {
  16. private readonly ILogger<ImageController> _logger;
  17. private readonly IClaimsAccessor _claims;
  18. /// <summary>
  19. /// 构造函数
  20. /// </summary>
  21. /// <param name="logger"></param>
  22. /// <param name="claims"></param>
  23. public ImageController(ILogger<ImageController> logger, IClaimsAccessor claims)
  24. {
  25. _logger = logger;
  26. _claims = claims;
  27. }
  28. /// <summary>
  29. /// 上传图片
  30. /// </summary>
  31. /// <param name="Files"></param>
  32. /// <returns></returns>
  33. [HttpPost("UploadImageAsync")]
  34. public async Task<ApiResult> UploadImageAsync(IFormCollection Files)
  35. {
  36. _logger.LogInformation("[开始] 上传图片信息.");
  37. try
  38. {
  39. //var form = Request.Form;//直接从表单里面获取文件名不需要参数
  40. string dd = Files["File"];
  41. var form = Files;//定义接收类型的参数
  42. Hashtable hash = new Hashtable();
  43. IFormFileCollection cols = Request.Form.Files;
  44. if (cols == null || cols.Count == 0)
  45. {
  46. return new ApiResult(ReturnCode.GeneralError, "没有上传文件");
  47. }
  48. foreach (IFormFile file in cols)
  49. {
  50. //定义图片数组后缀格式
  51. string[] LimitPictureType = { ".JPG", ".JPEG", ".GIF", ".PNG", ".BMP" };
  52. //获取图片后缀是否存在数组中
  53. string currentPictureExtension = Path.GetExtension(file.FileName).ToUpper();
  54. if (LimitPictureType.Contains(currentPictureExtension))
  55. {
  56. //为了查看图片就不在重新生成文件名称了
  57. var dateDirectory = DateTime.Now.ToString("yyyyMM");
  58. var relativePath = Path.Combine("wwwroot/uploads/images/", _claims.Linsence + "/" + dateDirectory);
  59. var directoryPath = Path.Combine(Directory.GetCurrentDirectory(), relativePath);
  60. var fileRelativePath = Path.Combine(relativePath + "/", file.FileName);
  61. if (!Directory.Exists(directoryPath))
  62. {
  63. Directory.CreateDirectory(directoryPath);
  64. }
  65. var path = Path.Combine(directoryPath + "/", file.FileName);
  66. using (var stream = new FileStream(path, FileMode.Create))
  67. {
  68. await Task.Run(()=>{
  69. //再把文件保存的文件夹中
  70. file.CopyTo(stream);
  71. hash.Add("file", "/" + fileRelativePath);
  72. });
  73. }
  74. }
  75. else
  76. {
  77. return new ApiResult(ReturnCode.GeneralError, "请上传指定格式的图片");
  78. }
  79. }
  80. return new ApiResult<Hashtable>(new Hashtable(hash));
  81. }
  82. catch (Exception ex)
  83. {
  84. return new ApiResult(ReturnCode.GeneralError, ex.Message + ex.InnerException);
  85. }
  86. }
  87. /// <summary>
  88. /// 上传系统图片
  89. /// </summary>
  90. /// <param name="Files"></param>
  91. /// <returns></returns>
  92. [HttpPost("UploadSysImageAsync")]
  93. public async Task<ApiResult> UploadSysImageAsync(IFormCollection Files)
  94. {
  95. try
  96. {
  97. //var form = Request.Form;//直接从表单里面获取文件名不需要参数
  98. string dd = Files["File"];
  99. var form = Files;//定义接收类型的参数
  100. Hashtable hash = new Hashtable();
  101. IFormFileCollection cols = Request.Form.Files;
  102. if (cols == null || cols.Count == 0)
  103. {
  104. return new ApiResult(ReturnCode.GeneralError, "没有上传文件");
  105. }
  106. foreach (IFormFile file in cols)
  107. {
  108. //定义图片数组后缀格式
  109. string[] LimitPictureType = { ".JPG", ".JPEG", ".GIF", ".PNG", ".BMP" };
  110. //获取图片后缀是否存在数组中
  111. string currentPictureExtension = Path.GetExtension(file.FileName).ToUpper();
  112. if (LimitPictureType.Contains(currentPictureExtension))
  113. {
  114. //为了查看图片就不在重新生成文件名称了
  115. var dateDirectory = DateTime.Now.ToString("yyyyMM");
  116. var relativePath = Path.Combine("wwwroot/uploads/images/", _claims.Linsence + "/sys/" + dateDirectory);
  117. var directoryPath = Path.Combine(Directory.GetCurrentDirectory(), relativePath);
  118. var fileRelativePath = Path.Combine(relativePath + "/", file.FileName);
  119. if (!Directory.Exists(directoryPath))
  120. {
  121. Directory.CreateDirectory(directoryPath);
  122. }
  123. var path = Path.Combine(directoryPath + "/", file.FileName);
  124. using (var stream = new FileStream(path, FileMode.Create))
  125. {
  126. await Task.Run(() => {
  127. //再把文件保存的文件夹中
  128. file.CopyTo(stream);
  129. hash.Add("file", "/" + fileRelativePath);
  130. });
  131. }
  132. }
  133. else
  134. {
  135. return new ApiResult(ReturnCode.GeneralError, "请上传指定格式的图片");
  136. }
  137. }
  138. return new ApiResult<Hashtable>(new Hashtable(hash));
  139. }
  140. catch (Exception ex)
  141. {
  142. return new ApiResult(ReturnCode.GeneralError, ex.Message + ex.InnerException);
  143. }
  144. }
  145. /// <summary>
  146. /// 上传巡检系统图片
  147. /// </summary>
  148. /// <param name="Files"></param>
  149. /// <returns></returns>
  150. [HttpPost("UploadIspImageAsync")]
  151. public async Task<ApiResult> UploadIspImageAsync(IFormCollection Files)
  152. {
  153. try
  154. {
  155. //var form = Request.Form;//直接从表单里面获取文件名不需要参数
  156. string dd = Files["File"];
  157. var form = Files;//定义接收类型的参数
  158. Hashtable hash = new Hashtable();
  159. IFormFileCollection cols = Request.Form.Files;
  160. if (cols == null || cols.Count == 0)
  161. {
  162. return new ApiResult(ReturnCode.GeneralError, "没有上传文件");
  163. }
  164. foreach (IFormFile file in cols)
  165. {
  166. //定义图片数组后缀格式
  167. string[] LimitPictureType = { ".JPG", ".JPEG", ".GIF", ".PNG", ".BMP" };
  168. //获取图片后缀是否存在数组中
  169. string currentPictureExtension = Path.GetExtension(file.FileName).ToUpper();
  170. if (LimitPictureType.Contains(currentPictureExtension))
  171. {
  172. //为了查看图片就不在重新生成文件名称了
  173. var dateDirectory = DateTime.Now.ToString("yyyyMM");
  174. var relativePath = Path.Combine("wwwroot/uploads/images/", _claims.Linsence + "/isp/" + dateDirectory);
  175. var directoryPath = Path.Combine(Directory.GetCurrentDirectory(), relativePath);
  176. var fileRelativePath = Path.Combine(relativePath + "/", file.FileName);
  177. if (!Directory.Exists(directoryPath))
  178. {
  179. Directory.CreateDirectory(directoryPath);
  180. }
  181. var path = Path.Combine(directoryPath + "/", file.FileName);
  182. using (var stream = new FileStream(path, FileMode.Create))
  183. {
  184. await Task.Run(() => {
  185. //再把文件保存的文件夹中
  186. file.CopyTo(stream);
  187. hash.Add("file", "/" + fileRelativePath);
  188. });
  189. }
  190. }
  191. else
  192. {
  193. return new ApiResult(ReturnCode.GeneralError, "请上传指定格式的图片");
  194. }
  195. }
  196. return new ApiResult<Hashtable>(new Hashtable(hash));
  197. }
  198. catch (Exception ex)
  199. {
  200. return new ApiResult(ReturnCode.GeneralError, ex.Message + ex.InnerException);
  201. }
  202. }
  203. }
  204. }