当前位置:网站首页>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
}
}
边栏推荐
- Bytes and Characters and Common Encodings
- go web之响应用户
- A concise tutorial on expanding (increasing capacity) of VMWare Esxi virtual system data storage
- 播放器的一些改进
- Dubins curve study notes and related thinking
- 面试突击72:输入URL之后会执行什么流程?
- d实验新异常
- 机器学习模型太慢?来看看英特尔(R) 扩展加速
- 一个用来装逼的利器
- Flutter 游戏教程之使用 Flutter 和 Flame 重现著名的 T-Rex 游戏
猜你喜欢
随机推荐
code inspection tool
01-MQ介绍以及产品比较
Mobile/Embedded-CV Model-2018: MobileFaceNets
Web优化躬行记(6)——优化闭环实践
ASP.NET Core 2.0中,解决大文件上传配置问题
idea installation steps
Excel method is commonly used in text function 5
xgboost 加速
COMSOL Multiphysics 6.0软件安装包和安装教程
定时任务框架Quartz-(一)Quartz入门与Demo搭建
2万字50张图玩转Flink面试体系
To make people's consumption safer, more assured and more satisfied
.net开发中,C# DateTime.Now 取出的时间含有星期解决办法
图像分割 总结
中原银行实时风控体系建设实践
小程序使用formdata格式传参
FreeSql 将 Saas 租户方案精简到极致[.NET ORM SAAS]
经开安监App技术服务支持
继承关系下构造方法的访问特点:
四、业务数据解析