当前位置:网站首页>在.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
}
}
边栏推荐
- 小程序使用formdata格式传参
- Is it safe to buy stocks with a straight flush?Will the funds be transferred?
- xgboost 加速
- 一个用来装逼的利器
- A small test of basic grammar, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, basic grammar of go lang and the use of variables EP02
- 【AGC】开放式测试示例
- 移动端/嵌入式-CV模型-2018:MobileFaceNets
- Android Studio关于MainActivity中的“import kotlinx.android.synthetic.main.activity_main.*”出现错误提示
- Redis 定长队列的探索和实践
- 【Collection】3. Wallpaper collection
猜你喜欢
随机推荐
利用图像二维熵实现视频信号丢失检测(Signal Loss Detection)
简单混合运算计算器
【数学知识】—— 质数/约数
VPP source address NAT
巧用Prometheus来扩展kubernetes调度器
实体List转为excel
VPP static mapping to realize DNAT
The keys of the Flutter storage database
【无标题】
Excel method is commonly used in text function 5
使用分类权重,轻松解决数据不平衡的问题
实例存储之shelve
牛客收藏上万的神作!这份阿里P8手写的MySQL主从原理手册真的牛
oracle中联表相关思考
等式变换(2015届华为校园招聘机试题第三题 )
推荐下载软件
d实验新异常
五、业务数据分析
LVS负载均衡群集及NAT模式群集
22-08-06 Xi'an EasyExcel implements dictionary table import and export