namespace AdService.Controller.Controllers { /// /// 采集文件操作接口 /// public class AdFileController : BaseController { ILogger logger { get; set; } /// /// 下载采集文件 /// /// 采集返回的名称 /// 采集返回的文件路径 /// [HttpGet] public IActionResult Download(string path, string fileName) { if (string.IsNullOrWhiteSpace(fileName)) { return NotFound("必须指定要下载的文件"); } if (fileName.StartsWith("/")) fileName = fileName.Substring(1); string localFile = Path.Combine(path, fileName); try { if (!System.IO.File.Exists(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) { logger.LogError($"文件[{localFile}]下载失败", ex); return NotFound($"文件[{fileName}]下载失败"); } } } }