上传文件WCF Web API端点 [英] Uploading files WCF Web API Endpoint

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

问题描述

有什么办法将文件上传到WCF的Web API端点?如果是这样,我怎么才能访问网络流来读取数据?



任何帮助,非常感激。

谢谢。

解决方案

假设您在客户端讨论:

<一个方法是创建一个从HttpContent派生的FileContent类。在覆盖中,当HTTPClient准备好接收流时,您将获得NetworkStream。



如果您询问服务器端,那么您可以简单地获得HTTPRequestMessage并访问httpRequestMessage.Content.ContentReadStream属性。





更新

默认情况下,WCF传输仅限于在65K发送消息。如果你想发送更大,你需要启用流式传输模式,你需要增加MaxReceivedMessageSize的大小,这就是为了防止有人通过上传大量文件来杀死你的服务器。
$ b

所以,你可以使用绑定配置来做到这一点,或者你可以在代码中做到这一点。这里是在代码中执行的一种方法:
$ b $ pre $ var $ endpoint $(HttpEndpoint) ; //假设一个端点
endpoint.TransferMode = TransferMode.Streamed;
endpoint.MaxReceivedMessageSize = 1024 * 1024 * 10; //允许文件高达10MB


Is there any way to upload a file to WCF web API endpoint? If so, how can I access Network Stream to read data?

Any help is very much appreciated.

Thanks.

解决方案

Assuming you are talking about on the client side:

One way to do it is to create a FileContent class that derives from HttpContent. In the overrides you will be given the NetworkStream when the HTTPClient is ready to receive the stream.

If you are asking about the server side, then you can simply get hold of the HTTPRequestMessage and access the httpRequestMessage.Content.ContentReadStream property.


Update

By default the WCF Transport is limited to sending messages at 65K. If you want to send larger you need to enable "Streaming Transfer Mode" and you need to increase the size of MaxReceivedMessageSize, which is there just as a guard to prevent someone killing your server by uploading a massive file.

So, you can do this using binding configuration or you can do it in code. Here is one way to do it in code:

var endpoint = ((HttpEndpoint)host.Description.Endpoints[0]);  //Assuming one endpoint
endpoint.TransferMode = TransferMode.Streamed;
endpoint.MaxReceivedMessageSize = 1024 * 1024 * 10;  // Allow files up to 10MB

这篇关于上传文件WCF Web API端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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