xwt
3 天以前 c8a9ab01f01ffdf522a5f174d684aff7722a9679
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using Microsoft.AspNetCore.Mvc;
using System.IO;
 
namespace MESApplication.Controllers
{
    [ApiController]
    [Route("Attachment")]
    public class AttachmentController : ControllerBase
    {
        // 根目录
        private readonly string ftpRootPath = @"D:\MES_FTP\IQC";
 
        [HttpGet("Download")]
        public IActionResult Download([FromQuery] string itemNo, [FromQuery] string fileName)
        {
            if (string.IsNullOrWhiteSpace(itemNo) || string.IsNullOrWhiteSpace(fileName))
                return BadRequest("物料编码和文件名不能为空");
 
            // 防止路径穿越攻击
            var safeItemNo = Path.GetFileName(itemNo.Trim());
            var safeFileName = Path.GetFileName(fileName.Trim());
 
            var filePath = Path.Combine(ftpRootPath, safeItemNo, safeFileName);
 
            if (!System.IO.File.Exists(filePath))
                return NotFound("文件不存在");
 
            var contentType = "application/octet-stream";
            return PhysicalFile(filePath, contentType, safeFileName);
        }
    }