ImgController.cs 4.2 KB

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