HMENetCore.Oss 6.0.48

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

HMENetCore.Oss

简介

HMENetCore.Oss 是一个本地文件存储服务封装库,提供了简单易用的API来处理文件的上传、下载、删除等操作。该库支持文件类型验证、大小限制、MD5校验、进度监控等功能,适合需要本地文件存储的各种应用场景。

功能特性

  • 支持文件上传(字节数组、流)
  • 支持同步/异步操作
  • 文件类型和大小验证
  • 自动目录分类(图片、音频、视频等)
  • MD5校验支持
  • 上传进度监控
  • 大文件分块处理
  • 自动创建目录结构

.NET支持

  • 跨平台支持: Supports .NET Standard 2.1, .NET 6, .NET 8, and .NET 9.

安装

dotnet add package HMENetCore.Oss --version 6.0.48

配置

添加配置到 appsettings.json

{
  "HmeOssConfig": {
    //本地上传文件路径(绝对路径)
    "LocalPath": "D:\\xx\\xx",
    //可访问文件的网络域名
    "NetworkPath": "https://xxxx.xx.com",
    //是否创建当前时间作为上级文件夹
    "IsDate": true,
    //允许上传的文件类型,为空时不限制类型 不为空例如:[".jpg",".txt",".xlsx"]
    "FileType": null,
    //允许上传的文件大小(Mb),为-1时不限制大小
    "FileSize": -1
  }
}

服务注册

builder.Services.AddHmeOssSetup(Configuration.GetSection("HmeOssConfig").Get<HmeOssConfig>());

// 或使用 Action 配置方式
builder.Services.AddHmeOssSetup(new HmeOssConfig
{
    LocalPath = "D:\\UploadFiles",         // 本地存储路径
    NetworkPath = "http://files.example.com", // 网络访问路径
    FileSize = 50,                         // 文件大小限制(MB)
    FileType = new List<string> { ".jpg", ".png", ".pdf" }, // 允许的文件类型
    IsDate = true                          // 是否按日期创建子目录
});

基础使用

1. 文件上传

//同步上传字节数组
var fileBytes = File.ReadAllBytes("test.jpg");
_hmeOssClient.WriteFile("test.jpg", fileBytes);

//异步上传流
using var stream = new FileStream("test.jpg", FileMode.Open);
await _hmeOssClient.WriteFileAsync("test.jpg", stream);

//自定义路径上传
var fileBytes = File.ReadAllBytes("test.jpg");
await _hmeOssClient.WriteFileAsync("D:\\CustomPath", "test.jpg", fileBytes);

2. 文件读取

//同步读取
byte[] fileData = _hmeOssClient.ReadFileByByte("test.jpg");

//异步读取
byte[] fileData = await _hmeOssClient.ReadFileByByteAsync("test.jpg");

//读取为流
Stream fileStream = _hmeOssClient.ReadFileByStream(fileBytes);

3. 文件管理

//检查文件是否存在
bool exists = _hmeOssClient.FileExists("test.jpg", "202508");

//删除文件
bool deleted = _hmeOssClient.DeleteFile("test.jpg", "202508");

4. 文件校验

// 从字节数组计算
string md5 = _hmeOssClient.GetFileMd5ByByte(fileBytes);

// 从流计算
string md5 = _hmeOssClient.GetFileMd5ByStream(stream);

// 从文件路径计算
string md5 = _hmeOssClient.GetFileMd5ByPath("D:\\test.jpg");

5. 目录搜索

// 搜索所有JPG文件
var jpgFiles = _hmeOssClient.GetFiles("D:\\UploadFiles", "\\.jpg$", true, false);

// 搜索所有目录
var directories = _hmeOssClient.GetDirectories("D:\\UploadFiles", null, true, false);

6. 错误处理

try
{
    await _hmeOssClient.WriteFileAsync("test.jpg", fileStream);
}
catch (ArgumentOutOfRangeException ex)
{
    // 处理文件大小超出限制
}
catch (NotSupportedException ex)
{
    // 处理不支持的文件类型
}
catch (IOException ex)
{
    // 处理IO异常
}

支持的文件类型分类:

picture: 图片 voice: 音频 video: 视频 document: 文档 archive: 压缩文件 executable: 可执行文件 other: 其他

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 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 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
6.0.48 138 8/11/2025
6.0.47 136 8/11/2025
6.0.46 212 8/7/2025
6.0.45 139 7/16/2025
6.0.43 155 6/18/2025
6.0.42 145 6/18/2025
6.0.40 273 6/10/2025
6.0.39 236 5/15/2025