using System.Dynamic; using MySqlX.XDevAPI.Common; using SqlSugar; using Tiobon.Core.DataAccess; namespace Tiobon.Core.Controllers; /// /// 文件服务 /// [Produces("application/json")] [Route("api/File"), ApiExplorerSettings(GroupName = Grouping.GroupName_Ghre)] public class FileController : BaseApiController { public ITiobonArticleServices _TiobonArticleServices { get; set; } private readonly ILogger _logger; /// /// 构造函数 /// /// /// public FileController(ILogger logger) { _logger = logger; } #region 上传图片 /// /// 上传图片 /// /// /// [HttpPost, Route("UploadImage")] public async Task> UploadImageAsync(IFormFileCollection fileList) { string filePath = null; using var _context = ContextFactory.CreateContext(); if (fileList.Count > 0) { filePath = !string.IsNullOrEmpty(filePath) ? filePath : "upload"; foreach (var file in fileList) { var ext = string.Empty; if (string.IsNullOrEmpty(file.FileName) == false) { var dotPos = file.FileName.LastIndexOf('.'); ext = file.FileName.Substring(dotPos + 1); } string pathHeader = "wwwroot/" + filePath; if (!Directory.Exists(pathHeader)) Directory.CreateDirectory(pathHeader); string fileName = StringHelper.GetSysID(); filePath = "/" + filePath + "/" + $"{fileName}.{ext}"; //var filepath = Path.Combine(pathHeader, file.FileName); using (var stream = global::System.IO.File.Create("wwwroot/" + filePath)) { await file.CopyToAsync(stream); } Ghre_Attachment fileAttachment = new Ghre_Attachment(); fileAttachment.Id = SnowFlakeSingle.Instance.NextId(); fileAttachment.AttachFileName = file.FileName; fileAttachment.CreateBy = App.User.ID; fileAttachment.CreateTime = DateTime.Now; fileAttachment.AttachmentName = fileName; fileAttachment.AttachFileExtension = ext; fileAttachment.AttachFileSize = file.Length; fileAttachment.PhysicsPath = filePath; fileAttachment.AttachmentType = file.ContentType; //url = fileName + "." + ext; _context.Add(fileAttachment); _context.SaveChanges(); } } return Success(filePath); } #endregion }