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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM