Files
wcs/WCS.WebApi/Controllers/FileDownLoadController.cs
hehaibing-1996 472862a978 1.库位禁用和启用、模组禁用启用
2.退出出库先解锁 后发指令
2024-05-21 18:46:51 +08:00

84 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.AspNetCore.Mvc;
using NPOI.HPSF;
using WCS.BLL.Services.IService;
using WCS.Model;
using WCS.Model.ApiModel;
using WCS.Model.ApiModel.User;
using WCS.Model.WebSocketModel;
namespace WCS.WebApi.Controllers
{
/// <summary>
/// 文件下载
/// </summary>
[ApiController]
[Route("[controller]")]
public class FileDownLoadController : ControllerBase
{
public FileDownLoadController()
{
}
[HttpGet("downloadApp")]
public IActionResult downloadApp(string fileName)
{
// 这里是文件的物理路径,你需要根据实际情况提供
var filePath = Path.Combine(Directory.GetCurrentDirectory(), $"Files/{fileName}");
// 检查文件是否存在
if (!System.IO.File.Exists(filePath))
{
return NotFound();
}
// 获取文件流
var stream = System.IO.File.OpenRead(filePath);
// 设置HTTP响应头使浏览器知道这是一个附件应该下载而不是打开
Response.Headers.Add("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// 返回文件流作为FileResult
return File(stream, "application/octet-stream", fileName);
}
[HttpGet("getLatestAppName")]
public ResponseCommon<string> getLatestAppName()
{
string directoryPath = Path.Combine(Directory.GetCurrentDirectory(), $"Files");
// 获取目录下的所有文件信息
FileInfo[] files = Directory.GetFiles(directoryPath, "*.APK")
.Select(file => new FileInfo(file))
.ToArray();
if (files == null || files.Length == 0)
{
return new ResponseCommon<string>()
{
Code = 201,
Message = "服务器不存在App安装包"
};
}
//获取最后修改的一个
FileInfo lastModifiedFile = files.OrderByDescending(f => f.LastWriteTime).First();
if (lastModifiedFile != null)
{
return new ResponseCommon<string>()
{
Code = 200,
Data = lastModifiedFile.Name
};
}
else
{
return new ResponseCommon<string>()
{
Code = 201,
Message = "服务器不存在App安装包"
};
}
}
}
}