如何通过WCF处理大文件上传?
我正在考虑将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想上传大文件,您肯定需要研究 WCF 流模式。
基本上,您可以更改绑定上的传输模式;默认情况下,它是缓冲的,即整个消息需要在发送方缓冲、序列化,然后作为一个整体传输。
使用流式传输,您可以定义单向流式传输(仅用于上传,仅用于下载)或双向流式传输。这是通过将绑定的传输模式设置为
StreamedRequest
、StreamedResponse
或简单的Streamed
来完成的。然后,您需要一个服务合同,该合同要么接收
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 plainStreamed
.Then you need to have a service contract which either receives a parameter of type
Stream
(for uploads), or returns a value of typeStream
(for downloads).That should do it!
MTOM 已优化为处理大型二进制数据。
MTOM is optimized to handle large binary data.
您可以将 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.