Threem.File.UploaderKit 1.1.0

dotnet add package Threem.File.UploaderKit --version 1.1.0
                    
NuGet\Install-Package Threem.File.UploaderKit -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Threem.File.UploaderKit" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Threem.File.UploaderKit" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Threem.File.UploaderKit" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Threem.File.UploaderKit --version 1.1.0
                    
#r "nuget: Threem.File.UploaderKit, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Threem.File.UploaderKit@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Threem.File.UploaderKit&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Threem.File.UploaderKit&version=1.1.0
                    
Install as a Cake Tool

Threem.File.UploaderKit

A robust file upload solution for .NET applications that handles file storage in Azure Blob Storage with metadata management in either MongoDB or SQL Server.

Features

  • Dual Storage Support: Store files in Azure Blob Storage while maintaining metadata in either:

    • MongoDB (for document-based flexibility), or

    • SQL Server (for relational data needs)

  • Automatic Configuration: Simple setup with just one line of code

  • Comprehensive File Handling:

    • SHA256 hash generation

    • MIME type detection

    • File size tracking

    • Custom metadata support

  • Logging Integration: Built-in NLog support with configurable log directory

  • Demo Project Available: Check out the Threem.File.UploaderKit Demo API for a working example

Installation


dotnet add package Threem.File.UploaderKit

⚑ Quick Start

1. Configure appsettings.json


{

  "FileUploader": {

    "StoreType": "SqlServer", // or "MongoDB"

    "Mongo": {

      "ConnectionString":"your-mongodb-connection-string",

      "Database": "FileMetadataDB",

      "Collection": "FileMetadata"

    },
 
    "Sql": {

      "ConnectionString": "Your_SQL_Connection_String"

    },
 
    "AzureBlobConnectionString": "Your_Azure_Blob_Connection_String",

    "AzureBlobContainer": "your-container-name",

    "LogDirectory": "C:\\Logs\\FileUploader"

  }

}

βœ… Tip: Choose the StoreType based on where you want to store your file metadata (SQL Server or MongoDB).

2. Set up your Program.cs

 
var settings = builder.Configuration.GetSection("FileUploader").Get<FileUploaderSettings>();
 
// Add services to the container

builder.Services.AddControllers();
 
// Configure File Uploader with automatic setup

builder.Services.AddFileUploader(settings);

πŸ’‘ The AddFileUploader() method automatically reads your config and sets everything up.

3. Create your controller

Here’s how to implement an endpoint that accepts files and uploads them:


[ApiController]

[Route("[controller]")]

[Consumes("multipart/form-data")]

public class FileUploadController : ControllerBase

{

    private readonly IFileUploader _fileUploader;
 
    public FileUploadController(IFileUploader fileUploader)

    {

        _fileUploader = fileUploader;

    }
 
    [HttpPost("upload")]

    public async Task<IActionResult> Upload(

        [FromForm] IFormFile file,

        [FromQuery] string clientId)

    {

        await _fileUploader.UploadAsync(file, clientId, new Dictionary<string, object>

        {

            { "status", "uploaded" },

            { "uploadedBy", "API" }

        });

        return Ok(new { Message = "Upload successful" });

    }

}

πŸ“Œ You can enrich the uploaded file with metadata using the dictionary passed to UploadAsync.


βš™οΈ Configuration Options

πŸ“‹ FileUploaderSettings

| Property | Type | Required | Description |

| --------------------------- | ------ | -------- | ----------------------------------------------- |

| StoreType | enum | βœ… Yes | MongoDB or SqlServer |

| AzureBlobConnectionString | string | βœ… Yes | Azure Blob Storage connection string |

| AzureBlobContainer | string | βœ… Yes | Name of the target container |

| LogDirectory | string | βœ… Yes | Directory path for logs |

πŸ“‹ MongoDB Config (when StoreType = MongoDB)

| Property | Type | Required | Description |

| ------------------ | ------ | -------- | ------------------ |

| ConnectionString | string | βœ… Yes | MongoDB connection |

| Database | string | βœ… Yes | Database name |

| Collection | string | βœ… Yes | Collection name |

πŸ“‹ SQL Server Config (when StoreType = SqlServer)

| Property | Type | Required | Description |

| ------------------ | ------ | -------- | --------------------- |

| ConnectionString | string | βœ… Yes | SQL Server connection |


🧱 Database Schema Requirements

πŸ“‹ MongoDB

Automatically creates the required collection. No schema setup needed.

🧱 SQL Server

Create this table in your database:


CREATE TABLE FileMetadata (

    Id NVARCHAR(255) PRIMARY KEY,

    FileName NVARCHAR(255) NOT NULL,

    ClientId NVARCHAR(255) NOT NULL,

    BlobUri NVARCHAR(MAX) NOT NULL,

    UploadDate DATETIME2 NOT NULL,

    FileSize BIGINT NOT NULL,

    MimeType NVARCHAR(100) NOT NULL,

    SHA256 NVARCHAR(64) NOT NULL,

    AdditionalData NVARCHAR(MAX) NULL

);


πŸ“š Logging Configuration (NLog)

Add nlog.config to your project.

Register NLog in Program.cs:


builder.Logging.AddNLog("nlog.config");


For Console Application Use Code eg(appsettings Remains the same)

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Threem.File.UploaderKit;
using Threem.File.UploaderKit.Models;

// Create Host with DI and Configuration
using IHost host = Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((ctx, config) =>
            {
                config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
            })
            .ConfigureServices((ctx, services) =>
            {
                var settings = ctx.Configuration.GetSection("FileUploader").Get<FileUploaderSettings>();
                services.AddFileUploader(settings);
            })
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders();
                logging.SetMinimumLevel(LogLevel.Information);
                logging.AddNLog("nlog.config"); // βœ… Use NLog in console app
            })
            .Build();

        var logger = host.Services.GetRequiredService<ILogger<Program>>();
        var uploader = host.Services.GetRequiredService<IFileUploader>();

string clientId = "console-client-001";

// 1️⃣ Upload via Stream
var filePath = @"C:\Temp\example.docx";
await using (var stream = File.OpenRead(filePath))
{
    await uploader.UploadAsync(stream, Path.GetFileName(filePath), clientId, new Dictionary<string, object>
    {
        { "status", "uploaded" },
        { "uploadedBy", "ConsoleApp-Stream" }
    });
}

// 2️⃣ Upload via byte[]
var fileBytes = await File.ReadAllBytesAsync(filePath);
await uploader.UploadAsync(fileBytes, Path.GetFileName(filePath), clientId, new Dictionary<string, object>
{
    { "status", "uploaded" },
    { "uploadedBy", "ConsoleApp-ByteArray" }
});

// 3️⃣ Upload via Base64
var base64String = Convert.ToBase64String(fileBytes);
await uploader.UploadAsync(base64String, Path.GetFileName(filePath), clientId, new Dictionary<string, object>
{
    { "status", "uploaded" },
    { "uploadedBy", "ConsoleApp-Base64" }
});

Console.WriteLine("βœ… All uploads completed successfully!");

🀝 Contributing

Contributions are welcome! Please:

  • Code follows existing style

  • New features include tests

  • Documentation is updated


🐞 Issues & Support

If you face any issues or need support, please reach out via email:

πŸ“§ info@threemsolutions.com


πŸ“œ License

This package is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 9 8/21/2025
1.0.0 169 8/11/2025

Version 1.1.0 - Release Notes
New Features and Improvements:

Upload from Console and Other Apps:
The upload method now supports file uploads directly from console applications, desktop apps, or anywhere byte array or stream or base64 string  can be provided β€” no longer just limited to API scenarios with IFormFile.

Upsert Functionality:
When uploading a file with the same file name and client ID, the package now updates the existing record instead of inserting a duplicate. This upsert behavior applies to both MongoDB and SQL Server metadata stores, ensuring data consistency.

Postman Friendly:
The NuGet package supports easy testing via Postman. For Swagger UI usage, please make sure to use appropriate DTOs to pass parameters correctly.



Previous Behavior:

Uploading was limited to API endpoints accepting IFormFile.

New uploads always inserted metadata, even if a file with the same name and client ID existed, resulting in duplicates.

Benefits:

More flexible integration across different app types.

Avoids duplicate metadata entries and keeps uploads clean and consistent.

Simplifies API testing and debugging with Postman.