1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
-
- namespace AdService.Controller.Controllers
- {
- /// <summary>
- /// 采集文件操作接口
- /// </summary>
- public class AdFileController : BaseController
- {
- ILogger logger { get; set; }
- /// <summary>
- /// 下载采集文件
- /// </summary>
- /// <param name="fileName">采集返回的名称</param>
- /// <param name="path">采集返回的文件路径</param>
- /// <returns></returns>
- [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}]下载失败");
- }
- }
- }
- }
|