using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using System.Web; using Bowin.Common.Utility; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace YLShipBuildLandMap.Web.Controllers.SystemSetting { [Route("api/systemsetting/[controller]")] [ApiController] public class ExcelController : ControllerBase { [HttpGet] public ActionResult Get(string fileName, string fileNewName) { fileNewName = HttpUtility.UrlDecode(fileNewName); if (fileName.Contains("../") || fileName.Contains("..\\")) { throw new Exception("不支持返回上级目录"); } var extName = Path.GetExtension(fileName); if (extName != ".xls" && extName != ".xlsx") { throw new Exception("只能下载Excel文件"); } var physicalPath = Path.Combine(Configuration.Current.AppSettings.TemplatePhysicalPath, fileName); var pdfStream = System.IO.File.Open(physicalPath, System.IO.FileMode.Open); var data = new byte[pdfStream.Length]; pdfStream.Read(data); pdfStream.Close(); var memoryStream = new MemoryStream(data); System.IO.File.Delete(physicalPath); return File(memoryStream, "application/vnd.ms-excel", fileNewName, false); } } }