You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.9 KiB
C#

11 months ago
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ZhongLianInterface;
using ZhongLianModel;
using ZhongLianService;
namespace ZhongLianAPI.Controllers
{
/// <summary>
/// 文件接口
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class DriveController : ControllerBase
{
public DriveController(IDriveService driveService)
{
DriveService = driveService;
}
private readonly IDriveService DriveService;
/// <summary>
/// 上传文件
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
[DisableRequestSizeLimit]
[HttpPost("upload")]
public async Task<ActionResult> 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);
}
}
/// <summary>
/// 获取文件
/// </summary>
/// <param name="id">编号</param>
/// <returns></returns>
[HttpGet("get")]
public async Task<ActionResult> Get([FromQuery] string id)
{
var FileItem = await DriveService.Get(id);
if (FileItem != null)
{
return File(FileItem.Data!, FileItem.ContentType!);
}
else
{
return BadRequest("下载失败");
}
}
}
}