DOPController.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Procurement.Enums;
  2. using Procurement.Models;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Mvc;
  6. using System;
  7. using System.ComponentModel.DataAnnotations;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. using Volo.Abp.AspNetCore.Mvc;
  12. namespace DigitalManufacturing.Controllers
  13. {
  14. public class DOPController : AbpController
  15. {
  16. private readonly IWebHostEnvironment _hostEnvironment;
  17. private readonly FileManager _fileManager;
  18. string[] pictureFormatArray = { ".png", ".jpg", ".jpeg", ".gif", ".PNG", ".JPG", ".JPEG", ".GIF" };
  19. public DOPController(
  20. IWebHostEnvironment hostEnvironment,
  21. FileManager fileManager)
  22. {
  23. _hostEnvironment = hostEnvironment;
  24. _fileManager = fileManager;
  25. }
  26. [HttpPost]
  27. [Route("api/digitalmanufacturing/feed")]
  28. //备料管理
  29. public async Task<ActionResult> Feed([Required]string name, IFormFile file)
  30. {
  31. string uniqueFileName = null;
  32. if (file != null)
  33. {
  34. //限制100M
  35. if (file.Length > 104857600)
  36. {
  37. return new BadRequestObjectResult("上传文件过大");
  38. }
  39. //文件格式
  40. var fileExtension = Path.GetExtension(file.FileName);
  41. if (!pictureFormatArray.Contains(fileExtension))
  42. {
  43. return new BadRequestObjectResult("上传文件格式错误");
  44. }
  45. var size = "";
  46. if (file.Length < 1024)
  47. size = file.Length.ToString() + "B";
  48. else if (file.Length >= 1024 && file.Length < 1048576)
  49. size = ((float)file.Length / 1024).ToString("F2") + "KB";
  50. else if (file.Length >= 1048576 && file.Length < 104857600)
  51. size = ((float)file.Length / 1024 / 1024).ToString("F2") + "MB";
  52. else size = file.Length.ToString() + "B";
  53. string uploadsFolder = Path.Combine(_hostEnvironment.WebRootPath, "Images");
  54. if (!Directory.Exists(uploadsFolder))
  55. {
  56. Directory.CreateDirectory(uploadsFolder);
  57. }
  58. uniqueFileName = Guid.NewGuid().ToString() + fileExtension;
  59. string filePath = Path.Combine(uploadsFolder, uniqueFileName);
  60. using (var fileStream = new FileStream(filePath, FileMode.Create))
  61. {
  62. file.CopyTo(fileStream);
  63. fileStream.Flush();
  64. }
  65. //TODO:文件md5哈希校验
  66. await _fileManager.Create(name, uniqueFileName, fileExtension, "", size, filePath, "/Images/" + uniqueFileName, FileType.Image);
  67. }
  68. return Ok(uniqueFileName);
  69. }
  70. [HttpPost]
  71. [Route("api/digitalmanufacturing/stock")]
  72. //库存管理
  73. public async Task<ActionResult> Stock([Required] string name, IFormFile file)
  74. {
  75. string uniqueFileName = null;
  76. if (file != null)
  77. {
  78. //限制100M
  79. if (file.Length > 104857600)
  80. {
  81. return new BadRequestObjectResult("上传文件过大");
  82. }
  83. //文件格式
  84. var fileExtension = Path.GetExtension(file.FileName);
  85. if (!pictureFormatArray.Contains(fileExtension))
  86. {
  87. return new BadRequestObjectResult("上传文件格式错误");
  88. }
  89. var size = "";
  90. if (file.Length < 1024)
  91. size = file.Length.ToString() + "B";
  92. else if (file.Length >= 1024 && file.Length < 1048576)
  93. size = ((float)file.Length / 1024).ToString("F2") + "KB";
  94. else if (file.Length >= 1048576 && file.Length < 104857600)
  95. size = ((float)file.Length / 1024 / 1024).ToString("F2") + "MB";
  96. else size = file.Length.ToString() + "B";
  97. string uploadsFolder = Path.Combine(_hostEnvironment.WebRootPath, "Images");
  98. if (!Directory.Exists(uploadsFolder))
  99. {
  100. Directory.CreateDirectory(uploadsFolder);
  101. }
  102. uniqueFileName = Guid.NewGuid().ToString() + fileExtension;
  103. string filePath = Path.Combine(uploadsFolder, uniqueFileName);
  104. using (var fileStream = new FileStream(filePath, FileMode.Create))
  105. {
  106. file.CopyTo(fileStream);
  107. fileStream.Flush();
  108. }
  109. //TODO:文件md5哈希校验
  110. await _fileManager.Create(name, uniqueFileName, fileExtension, "", size, filePath, "/Images/" + uniqueFileName, FileType.Image);
  111. }
  112. return Ok(uniqueFileName);
  113. }
  114. }
  115. }