wbc
2025-05-28 b9349673f1610970530db34ed8ed9b26e8e1e239
MESApplication/Controllers/QC/XJController.cs
@@ -495,4 +495,132 @@
            return ResponseResult.ResponseError(ex);
        }
    }
    //FTPLIST
    [HttpPost("ftpList")]
    public async Task<ResponseResult> getFtpList([FromBody] JObject data)
    {
        var itemno = data["itemno"]?.ToString();
        if (itemno == null || itemno == "") return new ResponseResult
        {
            status = 1,
            message = "未找到该产品的文件信息",
            data = "未找到该产品的文件信息"
        };
        var ftpAddress = "ftp://192.168.1.223:21";
        var username = "administrator";
        var password = "Rdyl8888";
        var remotePath = "PQC/XJ/" + itemno;
        try
        {
            dynamic resultInfos = new ExpandoObject();
            var ftpFiles = new SJService().GetFtpFileList(ftpAddress, username, password, remotePath);
            resultInfos.tbBillList = ftpFiles;
            return new ResponseResult
            {
                status = 0,
                message = "OK",
                data = resultInfos
            };
        }
        catch (Exception ex)
        {
            return ResponseResult.ResponseError(ex);
        }
    }
    [HttpPost("download")]
    public async Task<IActionResult> DownloadFile([FromBody] JObject data)
    {
        var fileName = data["fileName"]?.ToString();
        var itemno = data["itemno"]?.ToString();
        var ftpAddress = "ftp://192.168.1.223:21";
        var username = "administrator";
        var password = "Rdyl8888";
        var remotePath = "PQC/XJ/" + itemno;
        try
        {
            byte[] fileData = new SJService().DownloadFtpFile(ftpAddress, username, password, $"{remotePath}/{fileName}"); // 拼接完整路径
            // 生成临时访问 URL(示例逻辑)
            var tempFileId = Guid.NewGuid().ToString();
            // 将文件缓存到临时目录(实际项目需考虑清理机制)
            var tempPath = Path.Combine(Path.GetTempPath(), tempFileId);
            System.IO.File.WriteAllBytes(tempPath, fileData);
            // 返回可直接访问的 URL
            var baseUrl = $"{Request.Scheme}://{Request.Host}";
            return Ok(new ResponseResult
            {
                status = 0,
                message = "OK",
                data = new
                {
                    url = $"{baseUrl}/api/XJ/downloadTemp?fileId={tempFileId}&fileName={fileName}",
                    mimeType = GetMimeType(fileName)
                }
            });
        }
        catch (Exception ex)
        {
            return BadRequest(ResponseResult.ResponseError(ex));
        }
    }
    private string GetMimeType(string fileName)
    {
        string mimeType = "application/octet-stream";
        if (fileName.EndsWith(".jpg") || fileName.EndsWith(".jpeg"))
        {
            mimeType = "image/jpeg";
        }
        else if (fileName.EndsWith(".png"))
        {
            mimeType = "image/png";
        }
        else if (fileName.EndsWith(".gif"))
        {
            mimeType = "image/gif";
        }
        else if (fileName.EndsWith(".pdf"))
        {
            mimeType = "application/pdf";
        }
        else if (fileName.EndsWith(".doc") || fileName.EndsWith(".docx"))
        {
            mimeType = "application/msword";
        }
        else if (fileName.EndsWith(".xls") || fileName.EndsWith(".xlsx"))
        {
            mimeType = "application/vnd.ms-excel";
        }
        return mimeType;
    }
    [HttpGet("downloadTemp")]
    public IActionResult DownloadTemp(string fileId, string fileName)
    {
        var tempPath = Path.Combine(Path.GetTempPath(), fileId);
        if (!System.IO.File.Exists(tempPath))
        {
            return NotFound();
        }
        var fileBytes = System.IO.File.ReadAllBytes(tempPath);
        return File(fileBytes, GetMimeType(fileName), fileName);
    }
}