using Competition.Common.Util; using CompetitionAPI.Util; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace CompetitionAPI.Controllers.back.study { [Route("api/[controller]")] [ApiController] public class DeleteFileController : Controller { private readonly IWebHostEnvironment _webHostEnvironment; public DeleteFileController(IWebHostEnvironment webHostEnvironment) { _webHostEnvironment = webHostEnvironment; } /// /// 删除文件接口 /// /// 文件路径 /// [Authorize] [HttpGet] [APIFilter] public JsonResult Index(string Path) { try { var webRootPath = _webHostEnvironment.WebRootPath; if (System.IO.File.Exists(webRootPath + "/" + Path)) { var array = Path.Split('/'); if (array.Length > 0) { Array.Resize(ref array, array.Length - 1); } var new_path = string.Join("/", array); Tool.DeleteDir(webRootPath + new_path); //System.IO.File.Delete(webRootPath + "/" + Path); } return Json(Tool.GetJsonWithCode(APICode.Success, "删除成功")); } catch (Exception ex) { LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace); return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。")); } } } }