using Admin.Core.Common.Attributes; using Admin.Core.Common.Configs; using Admin.Core.Common.Output; using Microsoft.AspNetCore.Http; using System; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using FileInfo = Admin.Core.Common.Files.FileInfo; namespace Admin.Core.Common.Helpers { /// /// 文件上传帮助类 /// [SingleInstance] public class UploadHelper { /// /// 上传单文件 /// /// /// /// /// /// public async Task> UploadAsync(IFormFile file, FileUploadConfig config, object args, CancellationToken cancellationToken = default) { var res = new ResponseOutput(); if (file == null || file.Length < 1) { return res.NotOk("请上传文件!"); } //格式限制 if (!config.ContentType.Contains(file.ContentType)) { return res.NotOk("文件格式错误"); } //大小限制 if (!(file.Length <= config.MaxSize)) { return res.NotOk("文件过大"); } var fileInfo = new FileInfo(file.FileName, file.Length) { UploadPath = config.UploadPath, RequestPath = config.RequestPath }; var dateTimeFormat = config.DateTimeFormat.NotNull() ? DateTime.Now.ToString(config.DateTimeFormat) : ""; var format = config.Format.NotNull() ? StringHelper.Format(config.Format, args) : ""; fileInfo.RelativePath = Path.Combine(dateTimeFormat, format).ToPath(); if (!Directory.Exists(fileInfo.FileDirectory)) { Directory.CreateDirectory(fileInfo.FileDirectory); } fileInfo.SaveName = $"{IdWorkerHelper.GenId64()}.{fileInfo.Extension}"; await SaveAsync(file, fileInfo.FilePath, cancellationToken); return res.Ok(fileInfo); } /// /// 保存文件 /// /// /// /// /// public async Task SaveAsync(IFormFile file, string filePath, CancellationToken cancellationToken = default) { using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream, cancellationToken); } } } }