WPillar.FileManager 1.0.0

dotnet add package WPillar.FileManager --version 1.0.0
                    
NuGet\Install-Package WPillar.FileManager -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="WPillar.FileManager" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WPillar.FileManager" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="WPillar.FileManager" />
                    
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 WPillar.FileManager --version 1.0.0
                    
#r "nuget: WPillar.FileManager, 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 WPillar.FileManager@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=WPillar.FileManager&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=WPillar.FileManager&version=1.0.0
                    
Install as a Cake Tool

Pillar.FileManager

Pillar.FileManager 是一个功能强大的 .NET 文件管理库,提供了文件上传、下载、分片上传、断点续传等功能,适用于需要处理大文件和提供可靠文件存储的应用程序。

特性

  • 基本文件操作:上传、下载、删除文件
  • 大文件处理:支持分片上传和断点续传
  • 安全性:支持文件加密和解密
  • 性能优化:支持文件压缩和解压缩
  • 文件验证:支持文件类型验证和大小限制
  • 灵活存储:支持自定义存储目录和日期格式化路径
  • 元数据管理:提供完整的文件元数据

安装

通过 NuGet 包管理器安装:

dotnet add package Pillar.FileManager

快速开始

配置服务

在 Startup.cs 或 Program.cs 中配置服务:

services.AddFileManager(options =>
{
    options.StorageRoot = "uploads";                // 存储根目录
    options.MaxFileSize = 1024 * 1024 * 100;        // 最大文件大小 (100MB)
    options.AllowedExtensions = new[] { ".jpg", ".png", ".pdf" }; // 允许的文件类型
    options.ComputeHash = true;                     // 计算文件哈希值
    options.EnableCompression = false;              // 是否启用压缩
    options.EnableEncryption = false;               // 是否启用加密
    options.EncryptionPassword = "your-secret-key"; // 加密密钥
});

使用本地存储

简化的本地存储配置:

services.AddFileManagerWithLocalStorage(
    storageRoot: "uploads",
    maxFileSize: 100 * 1024 * 1024,  // 100MB
    allowedExtensions: new[] { ".jpg", ".png", ".pdf" }
);

自定义上传进度缓存

您可以通过实现 IUploadProgressCache 接口来自定义上传进度缓存:

一:创建自定义缓存并使用专用扩展方法
// 自定义 Redis 缓存实现
public class RedisUploadProgressCache : IUploadProgressCache
{
    
}

二:替换默认缓存实现
// 首先添加基本的文件管理服务
services.AddFileManager(options => 
{
    options.StorageRoot = "uploads";
});

// 然后注册自定义缓存实现,替换默认实现
services.AddSingleton<IUploadProgressCache, RedisUploadProgressCache>();

自定义存储路径

// 上传到按日期组织的目录
var result = await _fileStorage.SaveAsync(stream, "uploads/{yyyy}/{MM}/{dd}", file.FileName);

静态文件服务配置

基本配置

// 方式1:默认使用 wwwroot
app.UseStaticFiles();

// 方式2:添加额外的静态文件目录
app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(builder.Environment.ContentRootPath, "MyFiles")),
    RequestPath = "/files"
});

// 方式3:指定多个目录
app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new CompositeFileProvider(
        new PhysicalFileProvider(Path.Combine(builder.Environment.WebRootPath, "uploads")),
        new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "MyFiles"))
    )
});

防盗链配置

app.UseStaticFiles(new StaticFileOptions
{
    OnPrepareResponse = ctx =>
    {
        // 检查 Referer
        var referer = ctx.Context.Request.Headers.Referer.ToString();
        if (string.IsNullOrEmpty(referer) || !referer.StartsWith("http://localhost:5006"))
        {
            ctx.Context.Response.StatusCode = 403;
            ctx.Context.Response.Body = Stream.Null;
        }
    }
});

身份认证配置

app.UseStaticFiles(new StaticFileOptions
{
    OnPrepareResponse = ctx =>
    {
        // 检查认证状态
        if (!ctx.Context.User.Identity?.IsAuthenticated ?? true)
        {
            ctx.Context.Response.StatusCode = 401;
            ctx.Context.Response.Body = Stream.Null;
        }
    }
});

高级用法

分片上传

分片上传支持大文件上传和断点续传:

// 获取分片上传信息
var chunkInfo = new ChunkFileInfo
{
    FileName = "大文件.zip",
    ChunkSize = 1024 * 1024, // 1MB 分片大小
    TotalSize = fileStream.Length,
    FileMd5 = "文件MD5值"
};

// 上传分片
await _fileProcessor.UploadChunkAsync(chunkIndex, chunkStream, chunkInfo);

// 合并分片
var result = await _fileProcessor.MergeChunksAsync(chunkInfo);

许可证

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.0.0 147 5/4/2025