UploadController.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Cksoft.Unity;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. namespace WebUpload.Controllers
  10. {
  11. [Route("eap/api/[controller]/[action]")]
  12. public class UploadController : ControllerBase
  13. {
  14. //public IActionResult Index()
  15. //{
  16. // return View();
  17. //}
  18. private readonly IHostingEnvironment _hostingEnvironment;
  19. public UploadController(IHostingEnvironment hostingEnvironment)
  20. {
  21. _hostingEnvironment = hostingEnvironment;
  22. }
  23. [HttpPost]
  24. public EapResponse Uploadfile([FromBody] CmkFile json)
  25. {
  26. byte[] byteArray = json.Bt;
  27. string upLoadDir = AppConfigurtaionServices.Configuration["upload"];
  28. var dateNow = DateTime.Now;
  29. var fileNewName = "";
  30. if (json.Extension.Contains("xls") || json.Extension.Contains("xlsx"))
  31. {
  32. fileNewName = json.FileName + "." + json.Extension;
  33. }
  34. else
  35. {
  36. fileNewName = Guid.NewGuid().ToString() + "." + json.Extension;
  37. }
  38. Stream stream = new MemoryStream();
  39. try
  40. {
  41. string path = $"/Upload/{dateNow.Year}/{dateNow.Month}/{dateNow.Day}/";
  42. string phyicPath = _hostingEnvironment.WebRootPath + path;
  43. string savedPath = phyicPath + fileNewName;
  44. this.CreateDirectory(phyicPath);
  45. byte[] bs = byteArray;
  46. stream.Read(bs, 0, bs.Length);
  47. using (FileStream fs = new FileStream(savedPath, FileMode.Create))
  48. {
  49. fs.Write(bs, 0, bs.Length);
  50. return new EapResponse
  51. {
  52. Code = 1,
  53. Data = path + fileNewName,
  54. Msg = json.FileName + "," + upLoadDir
  55. };
  56. }
  57. }
  58. catch (Exception e)
  59. {
  60. return new EapResponse
  61. {
  62. Code = -1,
  63. Data = e.Message
  64. };
  65. }
  66. finally
  67. {
  68. stream.Close();
  69. stream.Dispose();
  70. }
  71. }
  72. [HttpGet]
  73. public EapResponse Getfile(string filename)
  74. {
  75. Stream stream = new MemoryStream();
  76. try
  77. {
  78. string phyicPath = _hostingEnvironment.WebRootPath+ filename;
  79. var base64 = Convert.ToBase64String(System.IO.File.ReadAllBytes(phyicPath));
  80. return new EapResponse
  81. {
  82. Code = 1,
  83. Data = base64
  84. };
  85. }
  86. catch (Exception e)
  87. {
  88. return new EapResponse
  89. {
  90. Code = -1,
  91. Data = e.Message
  92. };
  93. }
  94. finally
  95. {
  96. stream.Close();
  97. stream.Dispose();
  98. }
  99. }
  100. private void CreateDirectory(string path)
  101. {
  102. if (!Directory.Exists(path))
  103. {
  104. Directory.CreateDirectory(path);
  105. }
  106. }
  107. }
  108. }