前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >FastDFS.Client操作文件服务器

FastDFS.Client操作文件服务器

作者头像
guokun
发布2020-09-03 15:41:23
1.4K0
发布2020-09-03 15:41:23
举报
文章被收录于专栏:销声匿迹

1、配置文件设置

代码语言:javascript
复制
<configSections>
    <section name="fastdfs" type="FastDFS.Client.Config.FastDfsConfigurationSectionHandler, FastDFS.Client" />
  </configSections>

 <fastdfs>
    <FastDfsConfig GroupName="group1">
      <FastDfsServer IpAddress="192.168.88.103" Port="22122" />
    </FastDfsConfig>
  </fastdfs>

2、FastDFS.Client API调用

上传:

代码语言:javascript
复制
//文件保存到FastDFS服务器
var config = FastDfsManager.GetConfigSection();
ConnectionManager.InitializeForConfigSection(config);
StorageNode storageNode = FastDFSClient.GetStorageNode(config.GroupName);

string filePath = FastDFSClient.UploadFile(storageNode, file.Data, file.Extension.Replace(".", ""));        

下载:

代码语言:javascript
复制
private Task<byte[]> DownloadFileAsyn(string filePath)
        {
            return Task.Run(() =>
            {
                List<byte> content = new List<byte>();
                var config = FastDfsManager.GetConfigSection();
                ConnectionManager.InitializeForConfigSection(config);
                StorageNode storageNode = FastDFSClient.GetStorageNode(config.GroupName);
                FDFSFileInfo fileInfo = FastDFSClient.GetFileInfo(storageNode, filePath);
                if (fileInfo.FileSize >= 1024)  //文件内容大于1024字节时,需要分批下载
                {
                    long offset = 0, len = 1024;
                    while (len > 0)
                    {
                        byte[] buffer = FastDFSClient.DownloadFile(storageNode, filePath, offset, len);
                        content.AddRange(buffer);
                        offset += len;
                        len = Math.Min(fileInfo.FileSize - offset, 1024);
                    }
                }
                else
                {
                    content.AddRange(FastDFSClient.DownloadFile(storageNode, filePath));
                }

                return content.ToArray();
            });
        }

删除:

代码语言:javascript
复制
         // 从FastDFS服务器删除
            var config = FastDfsManager.GetConfigSection();
            ConnectionManager.InitializeForConfigSection(config);
            FastDFSClient.RemoveFile(config.GroupName, path);    
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-09-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档