using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using ZhongLianInterface; using ZhongLianModel; using ZhongLianService; namespace ZhongLianAPI.Controllers { /// /// 文件接口 /// [Route("api/[controller]")] [ApiController] public class DriveController : ControllerBase { public DriveController(IDriveService driveService) { DriveService = driveService; } private readonly IDriveService DriveService; /// /// 上传文件 /// /// /// [DisableRequestSizeLimit] [HttpPost("upload")] public async Task Upload(IFormFile file) { if (file == null) { return BadRequest("上传失败"); } var File = new FileDO(); File.FileName = file.FileName; File.ContentType = file.ContentType; File.Data = new byte[file.Length]; file.OpenReadStream().Read(File.Data, 0, (int)file.Length); var Msg = DriveService.InsertCheck(File); if (string.IsNullOrEmpty(Msg)) { return Ok(await DriveService.Insert(File)); } else { return BadRequest(Msg); } } /// /// 获取文件 /// /// 编号 /// [HttpGet("get")] public async Task Get([FromQuery] string id) { var FileItem = await DriveService.Get(id); if (FileItem != null) { return File(FileItem.Data!, FileItem.ContentType!); } else { return BadRequest("下载失败"); } } } }