当前位置:网站首页>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
}
}
边栏推荐
- Mobile/Embedded-CV Model-2017: MobelNets-v1
- A concise tutorial on expanding (increasing capacity) of VMWare Esxi virtual system data storage
- 文档数据库和列存储数据库有什么不同的嘛?
- Loadrunner12.0.2 installation and Chinese language pack installation (Chinese)
- Is it safe to buy stocks with a straight flush?Will the funds be transferred?
- .net开发中,C# DateTime.Now 取出的时间含有星期解决办法
- HMS Core分析服务智能运营6.5.1版本上线
- Recommend 100 nice English songs
- Pinia(一)初体验快速安装与上手
- Web优化躬行记(6)——优化闭环实践
猜你喜欢
随机推荐
NoSQL的意思就是就是不使用SQL吗?
图像分割 总结
VPP静态映射实现DNAT
面试突击72:输入URL之后会执行什么流程?
Feign应用及源码剖析
「每周译Go」这次我们来点不一样的!--《How to Code in Go》系列上线
Is it safe to buy stocks with a straight flush?Will the funds be transferred?
正确使用灯光 安全文明出行
COMSOL Multiphysics 6.0软件安装包和安装教程
Database Tuning: The Impact of Mysql Indexes on Group By Sorting
牛客收藏上万的神作!这份阿里P8手写的MySQL主从原理手册真的牛
经开安监App技术服务支持
PCL calculates the intersection of two straight lines in space
What is intrinsic safety?
Bytes and Characters and Common Encodings
[Deep Learning] Curriculum Learning
Dubins curve study notes and related thinking
MySQL源码解析之执行计划
一、用户数据仓库
idea installation steps









