AccessController : ControllerBase

目录

一、 AccessController : ControllerBase

二、 AccessController : ControllerBase

三、 AccessController : ControllerBase

四、 AccessController : ControllerBase

五、 AccessController : ControllerBase

  • AccessController : ControllerBase

namespace API.Controllers

{

    [Route("api/[controller]/[action]")]

    [ApiController]

    public class AccessController : ControllerBase

    {

        private readonly AccesoManager manager;

  • AccessController : ControllerBase

        public AccessController()

        {

            manager = new AccesoManager();

        }

  • AccessController : ControllerBase

        [HttpPost("create")]

        public ActionResult CreateAcceso(Acceso acceso)

        {

            manager.CreateAcceso(acceso);

            return Ok();

        }

  • AccessController : ControllerBase

        [HttpGet]

        public ActionResult<List<Acceso>> GetAllAccesos()

        {

            var accesos = manager.GetAllAccesos();

            return Ok(accesos);

        }

        [HttpPost("assign")]

        public ActionResult AssignAccessToRol(int rolID, int accesoID)

        {

            manager.AssignAccessToRol(rolID, accesoID);

            return Ok();

        }

  • AccessController : ControllerBase

        [HttpPost("remove")]

        public ActionResult RemoveAccessFromRol(int rolID, int accesoID)

        {

            manager.RemoveAccessFromRol(rolID, accesoID);

            return Ok();

        }

        [HttpGet("{rolID}")]

        public ActionResult<List<Acceso>> GetAccessByRol(int rolID)

        {

            var accesos = manager.GetAccessByRol(rolID);

            return Ok(accesos);

        }

    }

}

using System; using System.IO; using System.Linq; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace FileUpload.Controllers { [ApiController] public class UploadController : ControllerBase { private const int ChunkSize = 1024 * 1024 * 1; // 每个分片的大小,这里设为1MB private const string UploadPath = "uploads"; // 文件上传目录 private static string _filePath; // 完整的文件路径 [HttpPost("/upload/start")] public ActionResult StartUpload(IFormFile file) { if (file == null || file.Length <= 0) { return BadRequest("请选择要上传的文件"); } // 生成文件名 string fileName = file.FileName; string fileExt = Path.GetExtension(fileName); string newFileName = Guid.NewGuid().ToString("N") + fileExt; // 生成文件存储目录 string dirPath = Path.Combine(Directory.GetCurrentDirectory(), UploadPath); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } // 生成文件路径 _filePath = Path.Combine(dirPath, newFileName); // 返回上传开始的响应 return Ok(new { FileName = newFileName, ChunkSize, }); } [HttpPost("/upload/append")] public ActionResult AppendUpload(string fileName, int chunkIndex, int chunks, IFormFile chunk) { byte[] buffer = new byte[ChunkSize]; int bytesRead = 0; int start = chunkIndex * ChunkSize; // 分片开始位置 int end = Math.Min(start + ChunkSize, (int)chunk.Length); // 分片结束位置 using (Stream stream = chunk.OpenReadStream()) { using (FileStream fileStream = new FileStream(_filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)) { fileStream.Position = start; while ((bytesRead = stream.Read(buffer, 0, Math.Min(buffer.Length, end - start))) > 0) { fileStream.Write(buffer, 0, bytesRead); start += bytesRead; } fileStream.Flush(true); } } // 检查是否所有分片都上传成功 int[] uploadedChunks = Directory.GetFiles(Path.GetDirectoryName(_filePath), $"{Path.GetFileName(_filePath)}_*").Select(filepath => int.Parse(filepath.Split('_')[1])).ToArray(); if (uploadedChunks.Length == chunks) { // 合并分片 using (FileStream fileStream = new FileStream(_filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)) { foreach (int index in uploadedChunks.OrderBy(i => i)) { string chunkPath = $"{_filePath}_{index}"; using (FileStream chunkStream = new FileStream(chunkPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { chunkStream.CopyTo(fileStream); } System.IO.File.Delete(chunkPath); // 删除已合并的分片 } fileStream.Flush(true); } } return Ok(); } } }
04-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GZM888888

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值