FileController.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using System.Web;
  7. using Bowin.Common.Files;
  8. using Bowin.Common.WebModels;
  9. using YLShipBuildLandMap.Entity;
  10. using YLShipBuildLandMap.Entity.ViewModel;
  11. using YLShipBuildLandMap.Services.Common;
  12. using Microsoft.AspNetCore.Authorization;
  13. using Microsoft.AspNetCore.Mvc;
  14. using System.Drawing;
  15. namespace YLShipBuildLandMap.Web.Controllers.Common
  16. {
  17. [Route("api/common/[controller]/[action]")]
  18. [Authorize]
  19. [ApiController]
  20. public class FileController : ControllerBase
  21. {
  22. private IFileService FileService { get; set; }
  23. public FileController(IFileService fileService)
  24. {
  25. FileService = fileService;
  26. }
  27. [HttpPost]
  28. public async Task<OkObjectResult> UploadFile([FromForm] Guid fileRefId, [FromForm] int fileType)
  29. {
  30. var files = Request.Form.Files;
  31. List<SysAttachment> fileList = new List<SysAttachment>();
  32. foreach (var formFile in files)
  33. {
  34. string filePath = await FileHelper.Upload(formFile);
  35. fileList.Add(new SysAttachment
  36. {
  37. AttachmentId = Guid.NewGuid(),
  38. Name = Path.GetFileNameWithoutExtension(formFile.FileName),
  39. Suffix = Path.GetExtension(formFile.FileName),
  40. Url = filePath,
  41. ReferenceId = fileRefId,
  42. Type = fileType,
  43. CreateTime = DateTime.Now,
  44. CreateUserId = LoginUser.Current.UserID
  45. });
  46. }
  47. await FileService.AddFile(fileList);
  48. return Ok(new { success = true, msg = "上传成功", item = string.Join(',', fileList.Select(s => s.AttachmentId)) });
  49. }
  50. [HttpPost]
  51. public async Task<OkObjectResult> UploadPicture([FromForm] Guid fileRefId, [FromForm] int fileType)
  52. {
  53. var files = Request.Form.Files;
  54. List<SysAttachment> fileList = new List<SysAttachment>();
  55. foreach (var formFile in files)
  56. {
  57. string filePath = await FileHelper.Upload(formFile);
  58. fileList.Add(new SysAttachment
  59. {
  60. AttachmentId = Guid.NewGuid(),
  61. Name = Path.GetFileNameWithoutExtension(formFile.FileName),
  62. Suffix = Path.GetExtension(formFile.FileName),
  63. Url = filePath,
  64. ReferenceId = fileRefId,
  65. Type = fileType,
  66. CreateTime = DateTime.Now,
  67. CreateUserId = LoginUser.Current.UserID
  68. });
  69. }
  70. await FileService.AddFile(fileList);
  71. return Ok(new { success = true, msg = "上传成功",item = string.Join(',', fileList.Select(s => s.Url)) });
  72. }
  73. [HttpGet]
  74. public ResultMessage GetFileList(int pageindex, int pagesize, Guid fileRefId, int? fileType)
  75. {
  76. return ResultMessage.Success(FileService.GetFileList(pageindex, pagesize, fileRefId, fileType));
  77. }
  78. [HttpGet]
  79. [AllowAnonymous]
  80. public FileStreamResult DownloadFile(Guid fileId)
  81. {
  82. var file = FileService.GetFileById(fileId).Result;
  83. var stream = FileHelper.GetFileStream(file.Url);
  84. var filename = HttpUtility.UrlEncode(file.Name + file.Suffix, System.Text.Encoding.GetEncoding("UTF-8"));
  85. Response.Headers.Add("Content-Disposition", "attachment;filename=" + filename);
  86. return new FileStreamResult(stream, "application/octet-stream");
  87. }
  88. [HttpGet]
  89. public async Task<ResultMessage> DeleteFile(Guid fileId)
  90. {
  91. return ResultMessage.Success(FileService.DeleteFileById(fileId));
  92. }
  93. [HttpGet]
  94. public async Task<ResultMessage> DelfileByRefID(Guid noDeleteFileId, Guid fileRefId, int? fileType)
  95. {
  96. return ResultMessage.Success(FileService.DelfileByRefID(noDeleteFileId, fileRefId, fileType));
  97. }
  98. [HttpGet]
  99. [AllowAnonymous]
  100. public ActionResult Get(string fileName, string fileNewName)
  101. {
  102. fileNewName = HttpUtility.UrlDecode(fileNewName);
  103. var physicalPath = Path.Combine(Configuration.Current.AppSettings.TemplatePhysicalPath, fileName);
  104. var pdfStream = System.IO.File.Open(physicalPath, System.IO.FileMode.Open);
  105. var data = new byte[pdfStream.Length];
  106. pdfStream.Read(data);
  107. pdfStream.Close();
  108. var memoryStream = new MemoryStream(data);
  109. System.IO.File.Delete(physicalPath);
  110. return File(memoryStream, "application/octet-stream", fileNewName, false);
  111. }
  112. [HttpGet]
  113. [AllowAnonymous]
  114. public ResultMessage<ImageBase64> GetImageBase64(Guid fileId)
  115. {
  116. var file = FileService.GetFileById(fileId).Result;
  117. string base64string = "";
  118. int height = 0;
  119. int width = 0;
  120. //打开文件
  121. //FileStream fileStream = new FileStream(file.Url, FileMode.Open, FileAccess.Read, FileShare.Read);
  122. var fileStream = FileServerManager.GetFileStream(file.Url);
  123. // 读取文件的 byte[]
  124. byte[] bytes = new byte[fileStream.Length];
  125. fileStream.Read(bytes, 0, bytes.Length);
  126. base64string = Convert.ToBase64String(bytes);
  127. System.Drawing.Image image = System.Drawing.Image.FromStream(fileStream);
  128. height = image.Height;
  129. width = image.Width;
  130. fileStream.Close();
  131. return ResultMessage.Success(new ImageBase64() { Base64Data = base64string, Height = height, Width = width });
  132. }
  133. public class ImageBase64
  134. {
  135. public string Base64Data { get; set; }
  136. public int Height { get; set; }
  137. public int Width { get; set; }
  138. }
  139. }
  140. }