基于TCP协议的WCF传输大文件怎么出现进度条

 我来答
星月小木木
推荐于2016-05-18 · TA获得超过3.2万个赞
知道大有可为答主
回答量:2.4万
采纳率:0%
帮助的人:9873万
展开全部
基于TCP协议的WCF传输大文件如何出现进度条
RT.
比如传输50M的文件,
我能显示个进度条 2000Kb of 5000Kb 已传输40%.
有回复了加分.

------解决方案--------------------
不建议用WCF做文件传输
参考以下代码(VS2008下测试通过)
Service端:
C# codeusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.IO;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
namespace FSDownloadService
{
[MessageContract]
public class MyFileInfo
{
[MessageHeader]
public string FileName;
[MessageHeader]
public long FileSize;
[MessageBodyMember]
public Stream Stream;
public MyFileInfo() { }
public MyFileInfo(Stream stream, string fileName, long fileSize)
{
this.Stream = stream;
this.FileSize = fileSize;
this.FileName = fileName;
}
}

[MessageContract]
public class DownloadFileRequest
{
[MessageBodyMember]
public readonly string FileName;
public DownloadFileRequest() { }
public DownloadFileRequest(string fileName)
{
this.FileName = fileName;
}
}
[ServiceContract]
public interface IFileManager
{
[OperationContract]
MyFileInfo DownloadFile(DownloadFileRequest request);
}
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyService : IFileManager
{
public MyFileInfo DownloadFile(DownloadFileRequest request)
{
FileInfo fi = new FileInfo(request.FileName);
MyFileInfo result = new MyFileInfo(File.OpenRead(request.FileName), request.FileName, fi.Length);
return result;
}
}

public class MyHost
{
static ServiceHost host = null;
public static void Open()
{
string baseAddress = "net.tcp://localhost:2008/FileService";
host = new ServiceHost(typeof(MyService), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(IFileManager), GetTcpBinding(), "");
host.Open();
}
public static void Close()
{
if (host != null && host.State == CommunicationState.Opened)
{
host.Close();
}
host = null;
}
public static Binding GetTcpBinding()
{
NetTcpBinding binding = new NetTcpBinding();
binding.TransferMode = TransferMode.Streamed;
binding.MaxReceivedMessageSize = int.MaxValue;
return binding;
}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式