当前位置:网站首页>In the.net core, the use of c # realize fastdfs batch file upload more
In the.net core, the use of c # realize fastdfs batch file upload more
2022-08-08 10:02:00 【Drinking Water Siyuan 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;
//获取FormDataMulti-file information
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>
/// Get storage node
/// </summary>
/// <returns></returns>
public static StorageNode GetStorageNode()
{
//===========================初始化========================================
var trackerIPs = new List<IPEndPoint>();
// 只能指定IP,Setting up a domain name requires other means as a translation
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
}
}
边栏推荐
猜你喜欢
idea installation steps
Feign application and source code analysis
Loadrunner的录制event为0的问题解决方法与思路
STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
Practical Case: Building Churn Prediction Models with PySpark ML
Web optimization experience (6) - optimization closed-loop practice
功夫再高也怕菜刀,产品经理的那些事
COMSOL Multiphysics 6.0软件安装包和安装教程
snmptrapd+snmptt接收告警并用py脚本发送
Pinia(一)初体验快速安装与上手
随机推荐
VPP源地址NAT
MySQL redo log和undo log
[Deep Learning] Curriculum Learning
d实验新异常
Using classification weights, it is easy to solve the problem of data imbalance
Multi-scalar multiplication: state of the art & new ideas
快速定位线上慢 SQL 问题,掌握这几个性能排查工具可助你一臂之力
移动端/嵌入式-CV模型-2017:MobelNets-v1
LVS负载均衡群集
移动端/嵌入式-CV模型-2018:MobileFaceNets
Database Tuning: The Impact of Mysql Indexes on Group By Sorting
移动端/嵌入式-CV模型-2018:MobelNets-v2
C# - var 关键字
COMSOL Multiphysics 6.0 software installation package and installation tutorial
移动端/嵌入式-CV模型-2019:MobelNets-v3
定时任务框架Quartz-(一)Quartz入门与Demo搭建
Vulnhub靶机:GEMINI INC_ 1
SVN基本操作--文字解析版
FreeSql 将 Saas 租户方案精简到极致[.NET ORM SAAS]
2万字50张图玩转Flink面试体系