using Competition.Common.Util; using CompetitionAPI.Util; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace CompetitionAPI.Controllers.back.system { [Route("api/[controller]")] [ApiController] public class DownloadTemplateController : Controller { private readonly IWebHostEnvironment _webHostEnvironment; public DownloadTemplateController(IWebHostEnvironment webHostEnvironment) { _webHostEnvironment = webHostEnvironment; } /// /// 下载模板接口 /// /// 模块路径 /// [Authorize] [HttpGet] [APIFilter] public ActionResult Index(string FileName) { try { string web_path = _webHostEnvironment.WebRootPath; var filePath = web_path + "/" + FileName; //以字符流的形式下载文件 FileStream fs = new FileStream(filePath, System.IO.FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); string displayName = Path.GetFileName(filePath); return File(bytes, "application/octet-stream", displayName); } catch (Exception ex) { LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace); return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。")); } } } }