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

85 lines
2.8 KiB

using System.Dynamic;
using MySqlX.XDevAPI.Common;
using SqlSugar;
using Tiobon.Core.DataAccess;
namespace Tiobon.Core.Controllers;
/// <summary>
/// 文件服务
/// </summary>
[Produces("application/json")]
[Route("api/File"), ApiExplorerSettings(GroupName = Grouping.GroupName_Ghre)]
public class FileController : BaseApiController
{
public ITiobonArticleServices _TiobonArticleServices { get; set; }
private readonly ILogger<TiobonController> _logger;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="logger"></param>
///
public FileController(ILogger<TiobonController> logger)
{
_logger = logger;
}
#region 上传图片
/// <summary>
/// 上传图片
/// </summary>
/// <param name="fileList"></param>
/// <returns></returns>
[HttpPost, Route("UploadImage")]
public async Task<ServiceResult<string>> 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<string>(filePath);
}
#endregion
}