如何通过WCF处理大文件上传?

发布于 2024-08-15 09:38:46 字数 165 浏览 7 评论 0原文

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

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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无妨# 2024-08-22 09:38:46

如果您想上传大文件,您肯定需要研究 WCF 流模式

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

使用流式传输,您可以定义单向流式传输(仅用于上传,仅用于下载)或双向流式传输。这是通过将绑定的传输模式设置为 StreamedRequestStreamedResponse 或简单的 Streamed 来完成的。

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

然后,您需要一个服务合同,该合同要么接收 Stream 类型的参数(用于上传),要么返回 Stream 类型的值(用于下载)。

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

应该这样做!

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.

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>

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);
}

That should do it!

救赎№ 2024-08-22 09:38:46

MTOM 已优化为处理大型二进制数据。

MTOM is optimized to handle large binary data.

新一帅帅 2024-08-22 09:38:46

您可以将 webHttpBinding 与 TransferMode 流式传输和单个 Stream 参数或 Stream 响应(视情况而定)一起用于大型文件上传/下载,但您必须通过 URL 和/或标头发送任何请求元数据,除非您打算设计您自己在 Stream 上的框架。不过,您必须构建一个自定义的非 HTML 客户端(如 Silverlight、Flash 等),因为浏览器不支持对本地文件的随机访问,并且正常的文件上传将是表单发布,而不是 JSON。

You can use webHttpBinding with TransferMode streamed and a single Stream parameter or Stream response (as appropriate) for large file up/downloads, but you'd have to send any request metadata via URLs and/or headers, unless you're going to devise your own framing on the Stream. You'll have to build a custom non-HTML client (like Silverlight, Flash, etc) though, since browsers don't support random access to local files, and the normal file upload will be a form post, not JSON.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文