如何通过 WCF 处理大文件上传? [英] How to handle large file uploads via WCF?

查看:25
本文介绍了如何通过 WCF 处理大文件上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑将 WCF 用于一个项目,该项目需要人们能够将大文件 (64MB-1GB) 上传到我的服务器.我将如何使用 WCF 处理这个问题,可能具有恢复上传的能力.

I am looking into using WCF for a project which would require the ability for people to upload large files (64MB-1GB) to my server. How would I handle this with WCF, possibly with the ability to resume uploads.

为了处理更大的客户群,我想通过 WCF 测试 JSON.这将如何影响文件上传?可以从 JSON 完成,还是需要将上传部分切换到 REST?

In order to handle a larger client base, I wanted to test out JSON via WCF. How would this affect the file upload? Can it be done from JSON, or would they need to switch to REST for the upload portion?

推荐答案

如果你想上传大文件,你肯定需要查看 WCF 流模式.

If you want to upload large files, you'll definitely need to look into WCF Streaming Mode.

基本上,您可以更改绑定的传输模式;默认情况下,它是缓冲的,即整个消息需要在发送方缓冲,序列化,然后作为一个整体传输.

Basically, you can change the transfer mode on your binding; by default, it's buffered, i.e. the whole message needs to be buffered on the sender, serialized, and then transmitted as a whole.

通过流式传输,您可以定义单向流式传输(仅限上传、仅限下载)或双向流式传输.这是通过将绑定的 transferMode 设置为 StreamedRequestStreamedResponse 或只是简单的 Streamed 来完成的.

With Streaming, you can define either one-way streaming (for uploads only, for downloads only) or bidirectional streaming. This is done by setting the transferMode of your binding to StreamedRequest, StreamedResponse, or just plain Streamed.

<bindings>
   <basicHttpBinding>
      <binding name="HttpStreaming" 
               maxReceivedMessageSize="2000000"
               transferMode="StreamedRequest"/>
   </basicHttpBinding>
</bindings>

然后你需要有一个服务契约,它要么接收一个 Stream 类型的参数(用于上传),要么返回一个 Stream 类型的值(用于下载).

Then you need to have a service contract which either receives a parameter of type Stream (for uploads), or returns a value of type Stream (for downloads).

[ServiceContract]
public interface IFileUpload
{
    [OperationContract]
    bool UploadFile(Stream stream);
}

应该可以!

这篇关于如何通过 WCF 处理大文件上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆