using DW5S.WebApi; using Microsoft.AspNetCore.Mvc; namespace AdService.Controller.Controllers { /// /// 采集文件操作接口 /// public class AdFileController : BaseController { /// /// 下载采集的文件 /// /// 采集返回的名称 /// [HttpGet] public IActionResult Download(string fileName) { if (string.IsNullOrWhiteSpace(fileName)) { return NotFound("必须指定要下载的文件"); } if (fileName.StartsWith("/")) fileName = fileName.Substring(1); string localFile = Path.Combine("D:\\work", fileName); try { if (!System.IO.File.Exists(localFile)) { //IpsLogger.Error($"下载失败,文件[{localFile}]不存在"); return NotFound($"文件[{fileName}]不存在"); } var fileStream = new FileStream(localFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, 4096, true); //异步读取文件 return File(fileStream, "application/octet-stream", fileName, true); //为true时,支持断点续传 } catch (Exception ex) { //IpsLogger.Error($"文件[{localFile}]下载失败", ex); return NotFound($"文件[{fileName}]下载失败"); } } } }