From 25c42c17f69ffdbb48750172839f6b76296a8558 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Wed, 28 Oct 2026 00:05:14 +0330 Subject: [PATCH] add support for Upload IFormFille alongside by IFormFileCollection ... --- Interfaces/IXFileProvider.cs | 15 ++++++++ Providers/XFileProvider.cs | 67 ++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/Interfaces/IXFileProvider.cs b/Interfaces/IXFileProvider.cs index 601f376..1e04a78 100644 --- a/Interfaces/IXFileProvider.cs +++ b/Interfaces/IXFileProvider.cs @@ -76,6 +76,21 @@ namespace xFileService.Interfaces CancellationToken cancellationToken = default ); + /// + /// Upload File ... + /// + /// + /// + /// + /// + /// + Task Upload( + IFormFile file, + XUserClaimsInfoDto userInfo = null, + string connectionId = null, + CancellationToken cancellationToken = default + ); + /// /// Remove Specified Files ... /// diff --git a/Providers/XFileProvider.cs b/Providers/XFileProvider.cs index eae50b0..071972a 100644 --- a/Providers/XFileProvider.cs +++ b/Providers/XFileProvider.cs @@ -335,6 +335,73 @@ namespace xFileService.Providers return result; } + /// + /// Upload File ... + /// + /// + /// + /// + /// + /// + public virtual async Task Upload( + IFormFile file, + XUserClaimsInfoDto userInfo = null, + string connectionId = null, + CancellationToken cancellationToken = default + ) + { + // + // Validate Args ... + var isValid = !file.IsNull() && + !userInfo.IsNullOrDefault(); + if (!isValid) + { + XException.InvalidArgs.Throw(); + } + + // + // Save Files On Server Side Physically ... + // and Validate it ... + var uploadResult = await storageProvider + .HandleFileSave(file, cancellationToken); + isValid = !uploadResult.IsNullOrDefault(); + if (!isValid) + { + XException.ActionFailed.Throw(); + } + + // + // Prepare Result ... + // + var fileType = storageProvider.GetFileType(uploadResult.FileName); + + // + // Create Item Model ... + var item = new XFileDto + { + Type = fileType, + OwnerId = userInfo.UserId, + FileName = uploadResult.Name, + Name = uploadResult.FileName, + Path = uploadResult.FilePath, + Thumb = uploadResult.Thmbnail, + UploadedOn = DateTime.UtcNow, + ThumbPath = uploadResult.ThmbnailPath, + }; + + // + // Add Entity Model to DB ... + var result = await Add( + item: item, + userInfo: userInfo, + connectionId: connectionId, + cancellationToken: cancellationToken + ); + + // + return result; + } + /// /// Remove Specified Files ... ///