AdminFileController.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.IO;
  2. using System.Threading.Tasks;
  3. using GxPress.Common.Exceptions;
  4. using GxPress.Common.Tools;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Mvc;
  8. namespace GxPress.Api.AdminControllers
  9. {
  10. /// <summary>
  11. /// 文件管理
  12. /// </summary>
  13. [Route("api/admin/file")]
  14. [ApiController]
  15. public class AdminFileController : ControllerBase
  16. {
  17. private readonly IWebHostEnvironment _environment;
  18. public AdminFileController(IWebHostEnvironment environment)
  19. {
  20. _environment = environment;
  21. }
  22. /// <summary>
  23. /// 上传
  24. /// </summary>
  25. /// <param name="file"></param>
  26. /// <returns></returns>
  27. [HttpPost("upload")]
  28. public async Task<ResultPath> Upload(IFormFile file)
  29. {
  30. if (file == null) throw new BusinessException("请选择需要上传的文件");
  31. byte[] bytes;
  32. await using (var ms = new MemoryStream())
  33. {
  34. file.CopyTo(ms);
  35. bytes = ms.ToArray();
  36. }
  37. var saveResult = FileHelper.SaveFile(StringUtils.GetWebRootPath(_environment.WebRootPath), file.FileName,
  38. bytes);
  39. //saveResult.AbsolutePath ="https://" + Request.Host.Host +(Request.Host.Port > 0 ? $":{Request.Host.Port}" : string.Empty) +saveResult.RelativePath;
  40. saveResult.AbsolutePath = StringUtils.AddDomain(saveResult.RelativePath);
  41. return saveResult;
  42. }
  43. //上传文件到本地缓存文件夹,返回一个预览地址
  44. //真正提交的时候把缓存文件异步保存到七牛云
  45. //下载,从七牛云下载文件?
  46. /// <summary>
  47. /// 上传用户Excel
  48. /// </summary>
  49. /// <returns></returns>
  50. [HttpPost("upload-user-excel")]
  51. public async Task<ResultPath> UploadUserExcel(IFormFile file)
  52. {
  53. if (file == null) throw new BusinessException("请选择需要上传的文件");
  54. byte[] bytes;
  55. await using (var ms = new MemoryStream())
  56. {
  57. file.CopyTo(ms);
  58. bytes = ms.ToArray();
  59. }
  60. var saveResult = FileHelper.SaveFile(StringUtils.GetWebRootPath(_environment.WebRootPath), file.FileName,
  61. bytes);
  62. //saveResult.AbsolutePath = Request.Scheme + "://" + Request.Host.Host +(Request.Host.Port > 0 ? $":{Request.Host.Port}" : string.Empty) +saveResult.RelativePath;
  63. saveResult.AbsolutePath = StringUtils.AddDomain(saveResult.RelativePath);
  64. return saveResult;
  65. }
  66. }
  67. }