AdFileController.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 
  2. namespace AdService.Controller.Controllers
  3. {
  4. /// <summary>
  5. /// 采集文件操作接口
  6. /// </summary>
  7. public class AdFileController : BaseController
  8. {
  9. ILogger logger { get; set; }
  10. /// <summary>
  11. /// 下载采集文件
  12. /// </summary>
  13. /// <param name="fileName">采集返回的名称</param>
  14. /// <param name="path">采集返回的文件路径</param>
  15. /// <returns></returns>
  16. [HttpGet]
  17. public IActionResult Download(string path, string fileName)
  18. {
  19. if (string.IsNullOrWhiteSpace(fileName))
  20. {
  21. return NotFound("必须指定要下载的文件");
  22. }
  23. if (fileName.StartsWith("/"))
  24. fileName = fileName.Substring(1);
  25. string localFile = Path.Combine(path, fileName);
  26. try
  27. {
  28. if (!System.IO.File.Exists(localFile))
  29. {
  30. return NotFound($"文件[{fileName}]不存在");
  31. }
  32. var fileStream = new FileStream(localFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, 4096, true); //异步读取文件
  33. return File(fileStream, "application/octet-stream", fileName, true); //为true时,支持断点续传
  34. }
  35. catch (Exception ex)
  36. {
  37. logger.LogError($"文件[{localFile}]下载失败", ex);
  38. return NotFound($"文件[{fileName}]下载失败");
  39. }
  40. }
  41. }
  42. }