欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

分享WCF文件傳輸實現(xiàn)方法---WCFFileTransfer

 更新時間:2015年11月08日 15:03:09   投稿:mdxy-dxy  
這篇文章主要介紹了分享WCF文件傳輸實現(xiàn)方法---WCFFileTransfer,需要的朋友可以參考下

前幾天分享了分享了WCF聊天程序--WCFChat ,本文和大家一起分享利用WCF實現(xiàn)文件的傳輸。
程序運(yùn)行效果:
接收文件端:

發(fā)送文件端:連接WCF服務(wù),選擇要傳輸?shù)奈募?br />
文件傳輸成功:

我們會在保存文件的默認(rèn)路徑:C:\Documents and Settings\Administrator\桌面,下看到傳輸?shù)奈募?

代碼分析:
這里就不一一的闡述每一句代碼的作用了,感興趣的朋友可以下載,文后會有下載鏈接。說下值得注意的地方:
前兩天有人在百度知道中問能不能把WCF中的契約單獨(dú)封裝到一個類庫中,當(dāng)時感覺多此一舉,無意中看到把接口單獨(dú)分出去,有個很好的應(yīng)用,就是利用通道實現(xiàn)客戶端代理。
ITransfer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.Threading;
using System.IO;

namespace FileInterface
{
  [ServiceContract]
  public interface ITransfer
  {
    [OperationContract(Action = "UploadFile")]
    void TransferFile(FileTransferMessage request);//文件傳輸
  }


  [MessageContract]
  public class FileTransferMessage
  {
    [MessageHeader(MustUnderstand = true)]
    public string SavePath;//文件保存路徑

    [MessageHeader(MustUnderstand = true)]
    public string FileName;//文件名稱

    [MessageBodyMember(Order = 1)]
    public Stream FileData;//文件傳輸時間
  }
}

利用通道創(chuàng)建客戶端代理:

if (_proxy == null)
      {
        try
        {
          NetTcpBinding binding = new NetTcpBinding();
          binding.TransferMode = TransferMode.Streamed;
          binding.SendTimeout = new TimeSpan(0, 30, 0);
          //利用通道創(chuàng)建客戶端代理
          _proxy = ChannelFactory<ITransfer>.CreateChannel(binding, new EndpointAddress(CBSerURL.Text));
          IContextChannel obj = _proxy as IContextChannel;
          //string s = obj.SessionId;

        }
        catch (Exception ex)
        {
          MessageBox.Show(ex.Message);
          return;
        }

這樣,既不用添加服務(wù)引用,也不需要生成代理。
文件傳輸?shù)暮瘮?shù)不是很難,代碼如下:

public void TransferFile(FileTransferMessage request)
    {
      string logInfo;

      Program.Get_ILog().Log(logInfo = string.Format("開始接收文件,name={0}", request.FileName));//填寫日志
      //文件信息
      string uploadFolder = AppValue.GetParam()._saveDir;
      string savaPath = request.SavePath;
      string fileName = request.FileName;
      Stream sourceStream = request.FileData;
      FileStream targetStream = null;
      //判斷文件是否可讀
      if (!sourceStream.CanRead)
      {
        throw new Exception("數(shù)據(jù)流不可讀!");
      }
      if (savaPath == null) savaPath = @"文件傳輸\";
      if (!savaPath.EndsWith("\\")) savaPath += "\\";
      if (!uploadFolder.EndsWith("\\")) uploadFolder += "\\";

      uploadFolder = uploadFolder + savaPath;
      //創(chuàng)建保存文件夾
      if (!Directory.Exists(uploadFolder))
      {
        Directory.CreateDirectory(uploadFolder);
      }

      int fileSize = 0;
      string filePath = Path.Combine(uploadFolder, fileName);//Combine合并兩個路徑
      try
      {
        //文件流傳輸
        using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
        {
          //定義文件緩沖區(qū)
          const int bufferLen = 4096;
          byte[] buffer = new byte[bufferLen];
          int count = 0;

          while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
          {
            targetStream.Write(buffer, 0, count);
            fileSize += count;
          }
          targetStream.Close();
          sourceStream.Close();
        }
      }
      catch (Exception ex)
      {
        Program.Get_ILog().Log(logInfo + ex.Message);
      }

      Program.Get_ILog().Log(string.Format("接收文件完畢 name={0},filesize={1}",
       request.FileName, fileSize));
    }

其他的代碼感興趣的朋友下載來研究吧!


源代碼:WCFFileTransfer.rar

相關(guān)文章

最新評論