• FastDFS.Client操作文件服务器


    1、配置文件设置

    <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调用

    上传:

    //文件保存到FastDFS服务器
    var config = FastDfsManager.GetConfigSection();
    ConnectionManager.InitializeForConfigSection(config);
    StorageNode storageNode = FastDFSClient.GetStorageNode(config.GroupName);
    
    string filePath = FastDFSClient.UploadFile(storageNode, file.Data, file.Extension.Replace(".", ""));        

    下载:

    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();
                });
            }

    删除:

             // 从FastDFS服务器删除
                var config = FastDfsManager.GetConfigSection();
                ConnectionManager.InitializeForConfigSection(config);
                FastDFSClient.RemoveFile(config.GroupName, path);    
  • 相关阅读:
    weekly review 200930: Battlestar Galactica
    weekly review 200926: loss memory
    weekly review 200924: LOST
    转贴:对话守则
    weekly review 200928: Return
    推荐:继续聚焦小升初——破解奥数迷题
    Centos+Nginx部署Vue项目
    centos7安装nginx
    flaskmigrate 处理sqlite数据库报错Constraint must have a name 的解决方案
    将阿里矢量图添加到elementui
  • 原文地址:https://www.cnblogs.com/guokun/p/5843841.html
Copyright © 2020-2023  润新知