上一篇,了解了FastDFS是什么东东,通常稍微大一的网站都会作文件分离存储,FastDFS这轻型的分布式文件存储方式,很是有用。服务器
此图片截取博友(张占岭)的勿喷分布式
下面咱们就了解一下,FastDFS在.net平台下如何实现上传和下载功能。网站
咱用了WINFOM实现了一个简单的DEMO:spa
左侧为tracker能够分配多个,换行显示.net
右侧为storage列表,这里只作显示,能够不填orm
一、点击“链接FASTDFS”按钮,系统将链接fastdfs服务器blog
ConnectionManager.Initialize(trackerIPs); storageNode = FastDFSClient.GetStorageNode("group1");
若是Fastdfs服务器配置正确,你会看到“链接成功”的字样。图片
二、点击“选择文件并上传”按钮,文件将会把文件上传至fastdfs服务器get
OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "文本文件|*.*|png|*.png|JPG|*.jpg|JPEG|*.jpeg|全部文件|*.*"; openFileDialog.RestoreDirectory = true; openFileDialog.FilterIndex = 1; if (openFileDialog.ShowDialog() == DialogResult.OK) { string fName = openFileDialog.FileName; byte[] content = null; /* FileInfo fileInfo = new FileInfo(filePath); return fileInfo.Length;*/ FileStream streamUpload = new FileStream(fName, FileMode.Open); using (BinaryReader reader = new BinaryReader(streamUpload)) { content = reader.ReadBytes((int)streamUpload.Length); } textBox1.Text = fName; //主文件 string fileName = FastDFSClient.UploadFile(storageNode, content, "png"); var info = FastDFSClient.GetFileInfo(storageNode, fileName); //从文件 var slaveFileName = FastDFSClient.UploadSlaveFile("group1", content, fileName, "_120x120", "png"); var slaveInfo = FastDFSClient.GetFileInfo(storageNode, slaveFileName); listBox1.Items.Add(string.Format("主文件:http://{0}:8080/group1/{1}", trackerIPs[0].Address, fileName)); listBox1.Items.Add(string.Format("主文件大小:{0}KB,建立时间:{1}", info.FileSize, info.CreateTime)); listBox1.Items.Add(string.Format("从文件:http://{0}:8080/group1/{1}", trackerIPs[0].Address, slaveFileName)); listBox1.Items.Add(string.Format("从文件大小:{0}KB,建立时间:{1}", slaveInfo.FileSize, slaveInfo.CreateTime)); }
这时候你会在界面中看到你上传的文件返回的文件访问地址,你能够尝试在网页中打开,前提是上一篇的Nginx必定要配对。文件上传
对于“主文件”和“从文件”的概念,一般是用在图片文件的存储上,例如缩略图,就能够从文件进行存储。这个意义不是很大了解一下就行。
这个界面参考别的博友的,惋惜他没有代码,我帮他补上,小酌一下勿喷,哈哈