ExcelController.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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.Utility;
  8. using Microsoft.AspNetCore.Http;
  9. using Microsoft.AspNetCore.Mvc;
  10. namespace YLShipBuildLandMap.Web.Controllers.SystemSetting
  11. {
  12. [Route("api/systemsetting/[controller]")]
  13. [ApiController]
  14. public class ExcelController : ControllerBase
  15. {
  16. [HttpGet]
  17. public ActionResult Get(string fileName, string fileNewName)
  18. {
  19. fileNewName = HttpUtility.UrlDecode(fileNewName);
  20. if (fileName.Contains("../") || fileName.Contains("..\\"))
  21. {
  22. throw new Exception("不支持返回上级目录");
  23. }
  24. var extName = Path.GetExtension(fileName);
  25. if (extName != ".xls" && extName != ".xlsx")
  26. {
  27. throw new Exception("只能下载Excel文件");
  28. }
  29. var physicalPath = Path.Combine(Configuration.Current.AppSettings.TemplatePhysicalPath, fileName);
  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/vnd.ms-excel", fileNewName, false);
  37. }
  38. }
  39. }