当前位置:网站首页>在.net core中,利用C#实现fastdfs多文件批量上传
在.net core中,利用C#实现fastdfs多文件批量上传
2022-08-08 10:00:00 【饮水思源09】
在.net core中,利用C#实现fastdfs多文件批量上传
/// <summary>
/// 上传附件
/// </summary>
/// <returns></returns>
[RequestSizeLimit(1073741824)]
[HttpPost]
[Route("FileUpLoad")]
public ActionResult<IEnumerable<string>> UpLoadFile([FromForm]IFormCollection formCollection, string id)
{
try
{
bool flag = false;
//获取FormData中多文件信息
Microsoft.AspNetCore.Http.Internal.FormFileCollection filelist = (FormFileCollection)formCollection.Files;
foreach (var item in filelist)
{
if (item != null)
{
var storageNode = FileServerInit.GetStorageNode();
string type = item.FileName.Split(".").Last();
byte[] content = null;
//将文件转换为字节流
Stream fs = item.OpenReadStream();
using (BinaryReader reader = new BinaryReader(fs))
{
content = reader.ReadBytes((int)fs.Length);
}
string fileSize = FileServerInit.GetFileSize(content.Length);
string filePath = FastDFSClient.UploadFile(storageNode, content, type);
//........
}
}
//返回结果
}
catch (Exception e)
{
//异常处理
}
}
//文件初始化
public class FileServerInit
{
/// <summary>
/// 获取存储节点
/// </summary>
/// <returns></returns>
public static StorageNode GetStorageNode()
{
//===========================初始化========================================
var trackerIPs = new List<IPEndPoint>();
// 只能指定IP,设置域名需要其他方式作为转换
string IP = FileServer.GetFileServerIpAddress();
var endPoint = new IPEndPoint(IPAddress.Parse(IP), 22122);
trackerIPs.Add(endPoint);
ConnectionManager.Initialize(trackerIPs);
return FastDFSClient.GetStorageNode("group1");
}
//获取文件大小
public static string GetFileSize(long size)
{
var num = 1024.00; //byte
if (size < num)
return size + "B";
if (size < Math.Pow(num, 2))
return (size / num).ToString("f2") + "KB"; //kb
if (size < Math.Pow(num, 3))
return (size / Math.Pow(num, 2)).ToString("f2") + "MB"; //M
if (size < Math.Pow(num, 4))
return (size / Math.Pow(num, 3)).ToString("f2") + "G"; //G
return (size / Math.Pow(num, 4)).ToString("f2") + "TB"; //T
}
}
边栏推荐
- Flutter 游戏教程之使用 Flutter 和 Flame 重现著名的 T-Rex 游戏
- Multi-scalar multiplication: state of the art & new ideas
- [Image Classification] 2021-CoAtNet NeurlPS
- MySQL源码解析之执行计划
- hdu4635 Strongly connected (tarjan calculates strongly connected components + shrink points + ideas)
- Interviewer: Have you ever used a lock at work?Talk about the advantages, disadvantages and usage scenarios of optimistic locking and pessimistic locking
- Web optimization experience (6) - optimization closed-loop practice
- 面试突击72:输入URL之后会执行什么流程?
- 图像分割 总结
- xgboost 加速
猜你喜欢
随机推荐
业务缓存之体系化设计与开发
Mobile/Embedded-CV Model-2017: MobelNets-v1
开源一夏|Flutter实现搜索的三种方式
数据库不推荐使用外键的9个理由!
To make people's consumption safer, more assured and more satisfied
推荐100首好听英文歌
FreeSql 将 Saas 租户方案精简到极致[.NET ORM SAAS]
【收藏】3. 壁纸收藏
d实验新异常
IR(红外遥控)基本原理
xgboost 加速
[ 深度学习 ] 课程学习(Curriculum Learning)
斯坦福21秋季:实用机器学习【第5章】
小白求助,关于Go编译的顺序
Go 函数与方法
Multi-scalar multiplication: state of the art & new ideas
Redis 定长队列的探索和实践
Code implementation of various kinds of attention
MySQL redo log和undo log
LVS负载均衡群集









