ReportController.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Bowin.Common.Utility;
  7. using Microsoft.AspNetCore.Http;
  8. using Microsoft.AspNetCore.Mvc;
  9. namespace YLShipBuildLandMap.Web.Controllers.SystemSetting
  10. {
  11. [Route("api/systemsetting/[controller]")]
  12. [ApiController]
  13. public class ReportController : ControllerBase
  14. {
  15. [HttpGet]
  16. public ActionResult Get(string filePath)
  17. {
  18. if (!filePath.Contains(Configuration.Current.AppSettings.TemplatePath))
  19. filePath = $"{Configuration.Current.AppSettings.TemplatePath}/{filePath}";
  20. if (filePath.Contains("../") || filePath.Contains("..\\"))
  21. {
  22. throw new Exception("不支持返回上级目录");
  23. }
  24. var extName = Path.GetExtension(filePath);
  25. if (extName != ".pdf")
  26. {
  27. throw new Exception("只能下载Pdf文件");
  28. }
  29. var physicalPath = HttpHelper.MapPath(filePath);
  30. var pdfStream = System.IO.File.Open(physicalPath, System.IO.FileMode.Open);
  31. var data = new byte[pdfStream.Length];
  32. pdfStream.Read(data);
  33. pdfStream.Close();
  34. var memoryStream = new MemoryStream(data);
  35. System.IO.File.Delete(physicalPath);
  36. return File(memoryStream, "application/pdf");
  37. }
  38. }
  39. }