AdFileController.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using DW5S.WebApi;
  2. using Microsoft.AspNetCore.Mvc;
  3. namespace AdService.Controller.Controllers
  4. {
  5. /// <summary>
  6. /// 采集文件操作接口
  7. /// </summary>
  8. public class AdFileController : BaseController
  9. {
  10. /// <summary>
  11. /// 下载采集的文件
  12. /// </summary>
  13. /// <param name="fileName">采集返回的名称</param>
  14. /// <returns></returns>
  15. [HttpGet]
  16. public IActionResult Download(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("D:\\work", 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. }