我在查看这个帖子:如何通过WCF处理大文件上传?
我需要在我的提供商处托管一个 Web 服务,需要在其中上传和下载文件。 我们谈论的是从1MB到100MB的视频,因此需要流式传输方法。
我无法使其正常工作,我声明了一个接口:
[ServiceContract]
public interface IFileTransferService
{
[OperationContract]
void UploadFile(Stream stream);
}
一切都很好,我是这样实现的:
public string FileName = "test";
public void UploadFile(Stream stream)
{
try
{
FileStream outStream = File.Open(FileName, FileMode.Create, FileAccess.Write);
const int bufferLength = 4096;
byte[] buffer = new byte[bufferLength];
int count = 0;
while((count = stream.Read(buffer, 0, bufferLength)) > 0)
{
//progress
outStream.Write(buffer, 0, count);
}
outStream.Close();
stream.Close();
//saved
}
catch(Exception ex)
{
throw new Exception("error: "+ex.Message);
}
}
目前为止还没有问题,它已经发布到了我的互联网Web服务器上。一切都很顺利。
现在我要引用它并传递一个FileStream,但参数现在是byte[] - 为什么会这样,如何以正确的方式进行流处理?
编辑 我的绑定看起来像这样:
<bindings>
<basicHttpBinding>
<binding name="StreamingFileTransferServicesBinding"
transferMode="StreamedRequest"
maxBufferSize="65536"
maxReceivedMessageSize="204003200" />
</basicHttpBinding>
</bindings>
我可以无问题地使用它,没有错误 - 除了我的输入参数已从流更改为byte[]
编辑2 问题解决了!当我引用客户端时有点匆忙。我不小心做了一个“web reference”而不是一个“service reference”。这解决了byte[] / stream问题。