MESController.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 MESController : 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 MESController(
  20. IWebHostEnvironment hostEnvironment,
  21. FileManager fileManager)
  22. {
  23. _hostEnvironment = hostEnvironment;
  24. _fileManager = fileManager;
  25. }
  26. [HttpPost]
  27. [Route("api/digitalmanufacturing/mes/addreport")]
  28. //待收货看板
  29. //收货通知组织、收货通知日期、收货通知单号、收货申请人、单据类型、供应商编码、供应商名称、已等待收货时间、物料编码、物料名称、规格型号、数量
  30. public async Task<ActionResult> Feed([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. }
  72. }