QMSController.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 QMSController : 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 QMSController(
  20. IWebHostEnvironment hostEnvironment,
  21. FileManager fileManager)
  22. {
  23. _hostEnvironment = hostEnvironment;
  24. _fileManager = fileManager;
  25. }
  26. [HttpPost]
  27. [Route("api/digitalmanufacturing/qms/materialtracing")]
  28. //物料追溯
  29. //通过一个物料可以查询到关联物料的使用情况
  30. public async Task<ActionResult> MaterialTracing([Required]string name, IFormFile file)
  31. {
  32. string uniqueFileName = null;
  33. if (file != null)
  34. {
  35. //限制100M
  36. if (file.Length > 104857600)
  37. {
  38. return new BadRequestObjectResult("上传文件过大");
  39. }
  40. //文件格式
  41. var fileExtension = Path.GetExtension(file.FileName);
  42. if (!pictureFormatArray.Contains(fileExtension))
  43. {
  44. return new BadRequestObjectResult("上传文件格式错误");
  45. }
  46. var size = "";
  47. if (file.Length < 1024)
  48. size = file.Length.ToString() + "B";
  49. else if (file.Length >= 1024 && file.Length < 1048576)
  50. size = ((float)file.Length / 1024).ToString("F2") + "KB";
  51. else if (file.Length >= 1048576 && file.Length < 104857600)
  52. size = ((float)file.Length / 1024 / 1024).ToString("F2") + "MB";
  53. else size = file.Length.ToString() + "B";
  54. string uploadsFolder = Path.Combine(_hostEnvironment.WebRootPath, "Images");
  55. if (!Directory.Exists(uploadsFolder))
  56. {
  57. Directory.CreateDirectory(uploadsFolder);
  58. }
  59. uniqueFileName = Guid.NewGuid().ToString() + fileExtension;
  60. string filePath = Path.Combine(uploadsFolder, uniqueFileName);
  61. using (var fileStream = new FileStream(filePath, FileMode.Create))
  62. {
  63. file.CopyTo(fileStream);
  64. fileStream.Flush();
  65. }
  66. //TODO:文件md5哈希校验
  67. await _fileManager.Create(name, uniqueFileName, fileExtension, "", size, filePath, "/Images/" + uniqueFileName, FileType.Image);
  68. }
  69. return Ok(uniqueFileName);
  70. }
  71. [HttpPost]
  72. [Route("api/digitalmanufacturing/qms/inspection ")]
  73. //移动检验
  74. //所有检验方案支持移动端检验
  75. public async Task<ActionResult> Inspection([Required] string name, IFormFile file)
  76. {
  77. string uniqueFileName = null;
  78. if (file != null)
  79. {
  80. //限制100M
  81. if (file.Length > 104857600)
  82. {
  83. return new BadRequestObjectResult("上传文件过大");
  84. }
  85. //文件格式
  86. var fileExtension = Path.GetExtension(file.FileName);
  87. if (!pictureFormatArray.Contains(fileExtension))
  88. {
  89. return new BadRequestObjectResult("上传文件格式错误");
  90. }
  91. var size = "";
  92. if (file.Length < 1024)
  93. size = file.Length.ToString() + "B";
  94. else if (file.Length >= 1024 && file.Length < 1048576)
  95. size = ((float)file.Length / 1024).ToString("F2") + "KB";
  96. else if (file.Length >= 1048576 && file.Length < 104857600)
  97. size = ((float)file.Length / 1024 / 1024).ToString("F2") + "MB";
  98. else size = file.Length.ToString() + "B";
  99. string uploadsFolder = Path.Combine(_hostEnvironment.WebRootPath, "Images");
  100. if (!Directory.Exists(uploadsFolder))
  101. {
  102. Directory.CreateDirectory(uploadsFolder);
  103. }
  104. uniqueFileName = Guid.NewGuid().ToString() + fileExtension;
  105. string filePath = Path.Combine(uploadsFolder, uniqueFileName);
  106. using (var fileStream = new FileStream(filePath, FileMode.Create))
  107. {
  108. file.CopyTo(fileStream);
  109. fileStream.Flush();
  110. }
  111. //TODO:文件md5哈希校验
  112. await _fileManager.Create(name, uniqueFileName, fileExtension, "", size, filePath, "/Images/" + uniqueFileName, FileType.Image);
  113. }
  114. return Ok(uniqueFileName);
  115. }
  116. }
  117. }