12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- using DW5S.WebApi;
- using Microsoft.AspNetCore.Mvc;
- namespace AdService.Controller.Controllers
- {
- /// <summary>
- /// 采集文件操作接口
- /// </summary>
- public class AdFileController : BaseController
- {
- /// <summary>
- /// 下载采集的文件
- /// </summary>
- /// <param name="fileName">采集返回的名称</param>
- /// <returns></returns>
- [HttpGet]
- public IActionResult Download(string fileName)
- {
- if (string.IsNullOrWhiteSpace(fileName))
- {
- return NotFound("必须指定要下载的文件");
- }
- if (fileName.StartsWith("/"))
- fileName = fileName.Substring(1);
- string localFile = Path.Combine("D:\\work", fileName);
- try
- {
- if (!System.IO.File.Exists(localFile))
- {
- //IpsLogger.Error($"下载失败,文件[{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)
- {
- //IpsLogger.Error($"文件[{localFile}]下载失败", ex);
- return NotFound($"文件[{fileName}]下载失败");
- }
- }
- }
- }
|