0
0

ImgController.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Admin.Core.Model.Output;
  9. using Admin.Core.Attributes;
  10. namespace Admin.Core.Controllers.Admin
  11. {
  12. /// <summary>
  13. /// 图片管理
  14. /// </summary>
  15. [Area("Admin")]
  16. [Route("api/[area]/[controller]/[action]")]
  17. [ApiController]
  18. [NoOprationLog]
  19. public class ImgController : ControllerBase
  20. {
  21. /// <summary>
  22. /// 获取头像
  23. /// </summary>
  24. /// <param name="environment"></param>
  25. /// <param name="fileName"></param>
  26. /// <returns></returns>
  27. [HttpGet]
  28. [Route("{fileName}")]
  29. public FileStreamResult Avatar([FromServices]IWebHostEnvironment environment, string fileName = "")
  30. {
  31. string filepath = Path.Combine(environment.WebRootPath,"avatar", fileName);
  32. var stream = System.IO.File.OpenRead(filepath);
  33. string fileExt = Path.GetExtension(filepath);
  34. var contentTypeProvider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider();
  35. var contentType = contentTypeProvider.Mappings[fileExt];
  36. var fileDownloadName = Path.GetFileName(filepath);
  37. return File(stream, contentType, fileDownloadName);
  38. }
  39. /// <summary>
  40. /// 下载图片
  41. /// </summary>
  42. /// <param name="environment"></param>
  43. /// <param name="fileName"></param>
  44. /// <returns></returns>
  45. [HttpGet]
  46. [Route("{fileName}")]
  47. public FileStreamResult Download([FromServices]IWebHostEnvironment environment,string fileName = "")
  48. {
  49. string filepath = Path.Combine(environment.WebRootPath, "images", fileName);
  50. var stream = System.IO.File.OpenRead(filepath);
  51. string fileExt = Path.GetExtension(filepath);
  52. var contentTypeProvider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider();
  53. var contentType = contentTypeProvider.Mappings[fileExt];
  54. var fileDownloadName = Path.GetFileName(filepath);
  55. return File(stream, contentType, fileDownloadName);
  56. }
  57. /// <summary>
  58. /// 上传图片
  59. /// 支持多图片上传
  60. /// </summary>
  61. /// <param name="environment"></param>
  62. /// <returns></returns>
  63. [HttpPost]
  64. public async Task<IResponseOutput> Upload([FromServices]IWebHostEnvironment environment)
  65. {
  66. string path = string.Empty;
  67. string foldername = "images";
  68. IFormFileCollection files = null;
  69. try
  70. {
  71. files = Request.Form.Files;
  72. }
  73. catch (Exception)
  74. {
  75. files = null;
  76. }
  77. if (files == null || !files.Any())
  78. {
  79. return ResponseOutput.NotOk("请选择上传的文件。");
  80. }
  81. //格式限制
  82. var allowType = new string[] { "image/jpg", "image/png", "image/jpeg" };
  83. string folderpath = Path.Combine(environment.WebRootPath, foldername);
  84. if (!Directory.Exists(folderpath))
  85. {
  86. Directory.CreateDirectory(folderpath);
  87. }
  88. if (files.Any(c => allowType.Contains(c.ContentType)))
  89. {
  90. if (files.Sum(c => c.Length) <= 1024 * 1024 * 4)
  91. {
  92. //foreach (var file in files)
  93. var file = files.FirstOrDefault();
  94. string strpath = Path.Combine(foldername, DateTime.Now.ToString("MMddHHmmss") + file.FileName);
  95. path = Path.Combine(environment.WebRootPath, strpath);
  96. using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
  97. {
  98. await file.CopyToAsync(stream);
  99. }
  100. return ResponseOutput.Ok(strpath);
  101. }
  102. else
  103. {
  104. return ResponseOutput.NotOk("图片过大");
  105. }
  106. }
  107. else
  108. {
  109. return ResponseOutput.NotOk("图片格式错误");
  110. }
  111. }
  112. }
  113. }