Canducci.FileUpload
1.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Canducci.FileUpload --version 1.0.0
NuGet\Install-Package Canducci.FileUpload -Version 1.0.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="Canducci.FileUpload" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Canducci.FileUpload" Version="1.0.0" />
<PackageReference Include="Canducci.FileUpload" />
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 Canducci.FileUpload --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Canducci.FileUpload, 1.0.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 Canducci.FileUpload@1.0.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=Canducci.FileUpload&version=1.0.0
#tool nuget:?package=Canducci.FileUpload&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Canducci.FileUpload
A file upload library for ASP.NET Core with validation, flexible configuration, and support for synchronous and asynchronous operations.
Portugues: README_PTBR.md
Installation
Package Manager
Install-Package Canducci.FileUpload
.NET CLI
dotnet add package Canducci.FileUpload
PackageReference
<PackageReference Include="Canducci.FileUpload" Version="1.0.0" />
Configuration
Program.cs
Add the service in Program.cs:
using Canducci.FileUpload.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Add FileUpload with default configuration (random filename)
builder.Services.AddFileUpload();
// OR with custom configuration (GUID filename)
builder.Services.AddFileUpload(config =>
{
config.FileUploadConfigurationName = FileUploadConfigurationName.Guid;
});
var app = builder.Build();
Configuration Options
| Option | Description |
|---|---|
FileUploadConfigurationName.Random |
Generates random filename (default) |
FileUploadConfigurationName.Guid |
Generates GUID-based filename |
Controller Usage
Basic Example
using Canducci.FileUpload;
using Canducci.FileUpload.Interfaces;
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/[controller]")]
public class UploadController : ControllerBase
{
[HttpPost]
public IActionResult Upload(FileUpload fileUpload)
{
var result = fileUpload.Save("uploads", options =>
{
options.MaxFileSizeBytes = 5 * 1024 * 1024; // 5 MB
options.RejectEmptyFiles = true;
options.AllowedExtensions.Add(".jpg");
options.AllowedExtensions.Add(".png");
options.AllowedExtensions.Add(".pdf");
});
if (result.Success)
{
return Ok(new
{
Message = "Upload successful!",
Files = result.Files.Select(f => new
{
f.FileNameSent,
f.FileNameSave,
f.Extension,
f.Length,
f.Path
})
});
}
return BadRequest(new
{
Message = "Upload failed",
Errors = result.Files
.Where(f => !f.Success)
.Select(f => new
{
f.FileNameSent,
Error = f.Exception?.Message
})
});
}
}
Async Example
[HttpPost("async")]
public async Task<IActionResult> UploadAsync(FileUpload fileUpload, CancellationToken cancellationToken)
{
var result = await fileUpload.SaveAsync("uploads", options =>
{
options.MaxFileSizeBytes = 10 * 1024 * 1024; // 10 MB
options.RejectEmptyFiles = true;
}, cancellationToken);
if (result.Success)
{
return Ok(new { Message = "Async upload completed!" });
}
return BadRequest(new { Message = "Upload failed" });
}
Multiple Files Example
[HttpPost("multiple")]
public IActionResult UploadMultiple(FileUpload fileUpload)
{
if (fileUpload.Count == 0)
{
return BadRequest("No files uploaded");
}
var result = fileUpload.Save("documents", options =>
{
options.MaxFileSizeBytes = 20 * 1024 * 1024; // 20 MB
options.AllowedExtensions.Add(".docx");
options.AllowedExtensions.Add(".xlsx");
options.AllowedExtensions.Add(".pdf");
});
var successFiles = result.Files.Where(f => f.Success).ToList();
var failedFiles = result.Files.Where(f => !f.Success).ToList();
return Ok(new
{
TotalUploaded = fileUpload.Count,
Succeeded = successFiles.Count,
Failed = failedFiles.Count,
Files = successFiles.Select(f => f.FileNameSave),
Errors = failedFiles.Select(f => new { f.FileNameSent, Error = f.Exception?.Message })
});
}
Predefined Options Example
[HttpPost("images")]
public IActionResult UploadImages(FileUpload fileUpload)
{
var options = new FileUploadOptions
{
MaxFileSizeBytes = 2 * 1024 * 1024, // 2 MB
RejectEmptyFiles = true
};
options.AllowedExtensions.Add(".jpg");
options.AllowedExtensions.Add(".jpeg");
options.AllowedExtensions.Add(".png");
options.AllowedExtensions.Add(".gif");
options.AllowedExtensions.Add(".webp");
var result = fileUpload.Save("images", options);
return result.Success
? Ok(result.Files.Select(f => f.Path))
: BadRequest("Failed to save images");
}
Validation Options
| Option | Type | Default | Description |
|---|---|---|---|
MaxFileSizeBytes |
long |
10485760 (10 MB) | Maximum file size in bytes |
RejectEmptyFiles |
bool |
true |
Reject empty files (0 bytes) |
AllowedExtensions |
HashSet<string> |
Empty | Allowed extensions (empty = all allowed) |
Result Properties
IFileUploadResult
| Property | Type | Description |
|---|---|---|
Success |
bool |
true if all files were saved successfully |
Files |
IReadOnlyList<IFileItemResult> |
List of results per file |
IFileItemResult
| Property | Type | Description |
|---|---|---|
Success |
bool |
Whether the file was saved successfully |
FileNameSent |
string |
Original filename sent by client |
FileNameSave |
string |
Filename saved on server |
Extension |
string |
File extension |
Length |
long |
File size in bytes |
Directory |
string |
Directory where file was saved |
Path |
string |
Full file path |
Exception |
Exception? |
Exception if error occurred |
Error Messages
| Message | Description |
|---|---|
File is Empty |
Empty file and RejectEmptyFiles = true |
File is TooLarge |
File exceeds MaxFileSizeBytes |
ExtensionNotAllowed |
Extension not in AllowedExtensions |
Complete Controller Example
using Canducci.FileUpload;
using Canducci.FileUpload.Interfaces;
using Microsoft.AspNetCore.Mvc;
namespace MyApp.Controllers;
[ApiController]
[Route("api/[controller]")]
public class FilesController : ControllerBase
{
[HttpPost("upload")]
[RequestSizeLimit(50 * 1024 * 1024)] // 50 MB request limit
public async Task<IActionResult> Upload(
FileUpload fileUpload,
CancellationToken cancellationToken)
{
if (fileUpload.Count == 0)
{
return BadRequest(new { Error = "No files uploaded" });
}
var result = await fileUpload.SaveAsync("uploads", options =>
{
options.MaxFileSizeBytes = 10 * 1024 * 1024;
options.RejectEmptyFiles = true;
options.AllowedExtensions.Add(".jpg");
options.AllowedExtensions.Add(".jpeg");
options.AllowedExtensions.Add(".png");
options.AllowedExtensions.Add(".pdf");
options.AllowedExtensions.Add(".docx");
}, cancellationToken);
if (result.Success)
{
return Ok(new
{
Message = "Upload completed successfully",
Files = result.Files.Select(f => new
{
Original = f.FileNameSent,
Saved = f.FileNameSave,
Size = f.Length,
FullPath = f.Path
})
});
}
var errors = result.Files
.Where(f => !f.Success)
.Select(f => new
{
File = f.FileNameSent,
Error = f.Exception?.Message ?? "Unknown error"
});
return BadRequest(new
{
Message = "Some files failed to upload",
Errors = errors
});
}
}
Requirements
- .NET 8.0 or later
- ASP.NET Core
License
MIT License
| Product | Versions 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 is compatible. 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 is compatible. 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.
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Canducci.FileUpload:
| Package | Downloads |
|---|---|
|
Canducci.FileUpload.FluentValidation
FluentValidation integration for Canducci.FileUpload. |
GitHub repositories
This package is not used by any popular GitHub repositories.