AdFileController.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 
  2. namespace AdService.Controller.Controllers
  3. {
  4. /// <summary>
  5. /// 采集文件操作接口
  6. /// </summary>
  7. public class AdFileController : BaseController
  8. {
  9. /// <summary>
  10. /// 下载采集文件
  11. /// </summary>
  12. /// <param name="fileName">采集返回的名称</param>
  13. /// <param name="path">采集返回的文件路径</param>
  14. /// <returns></returns>
  15. [HttpGet]
  16. public IActionResult Download(string path, string fileName)
  17. {
  18. if (string.IsNullOrWhiteSpace(fileName))
  19. {
  20. return NotFound("必须指定要下载的文件");
  21. }
  22. if (fileName.StartsWith("/"))
  23. fileName = fileName.Substring(1);
  24. string localFile = Path.Combine(path, fileName);
  25. try
  26. {
  27. if (!System.IO.File.Exists(localFile))
  28. {
  29. //IpsLogger.Error($"下载失败,文件[{localFile}]不存在");
  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. //IpsLogger.Error($"文件[{localFile}]下载失败", ex);
  38. return NotFound($"文件[{fileName}]下载失败");
  39. }
  40. }
  41. }
  42. }