上传文件 WCF Web API 端点
有没有办法将文件上传到 WCF Web API 端点?如果是这样,我如何访问 Network Stream 来读取数据?
非常感谢任何帮助。
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您正在谈论客户端:
一种方法是创建一个派生自 HttpContent 的 FileContent 类。在覆盖中,当 HTTPClient 准备好接收流时,您将获得 NetworkStream。
如果您询问服务器端,那么您只需获取 HTTPRequestMessage 并访问 httpRequestMessage.Content.ContentReadStream 属性即可。
更新
默认情况下,WCF 传输仅限于以 65K 发送消息。如果你想发送更大的数据,你需要启用“流传输模式”,并且需要增加 MaxReceivedMessageSize 的大小,它的作用是防止有人通过上传大量文件来破坏你的服务器。
因此,您可以使用绑定配置来完成此操作,也可以使用代码来完成此操作。这是在代码中执行此操作的一种方法:
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:
此答案仅旨在通过 Web API 预览版 6 添加一些详细信息。
TransferMode
和MaxRecievedMessageSize
是通过WebApiConfigurationClass
新公开的。我喜欢它!
This answer is only designed to add some details with Web API preview 6.
TransferMode
andMaxRecievedMessageSize
are new exposed through theWebApiConfigurationClass
.I like it!