简体   繁体   English

如何使用WCF从客户端向服务器发送大文件?

[英]How to Send Large File From Client To Server Using WCF?

How to Send Large File From Client To Server Using WCF in C#? 如何在C#中使用WCF从客户端向服务器发送大文件? Below the configuration code. 在配置代码下面。

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="HttpStreaming_IStreamingSample" 
                         maxReceivedMessageSize="67108864"
                          transferMode="Streamed">
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint 
            address="http://localhost:4127/StreamingSample.svc"
            binding="basicHttpBinding" 
            bindingConfiguration="HttpStreaming_IStreamingSample"
            contract="StreamingSample.IStreamingSample" 
            name="HttpStreaming_IStreamingSample" />
    </client>
</system.serviceModel>

You need to check out streaming, as Dzmitry already pointed out. 正如Dzmitry已经指出的那样,你需要检查流媒体。

In order to be able to send large files as a stream to your service, you'll need to: 为了能够将大型文件作为流发送到您的服务,您需要:

  • create a service method that accepts a Stream as its input parameter 创建一个接受Stream作为其输入参数的服务方法
  • create a binding configuration (on both the server and the client) which uses transferMode=StreamedRequest 创建使用transferMode=StreamedRequest的绑定配置(在服务器和客户端上)
  • create a stream in your client and send it to the service method 在您的客户端中创建一个流并将其发送到服务方法

So first off, you need a method in your service contract: 首先,您需要在服务合同中使用一种方法:

[ServiceContract]
interface IYourFileService
{
   [OperationContract]
   void UploadFile(Stream file)
}

Then you need a binding configuration: 然后你需要一个绑定配置:

<bindings>
  <basicHttpBinding>
    <binding name="FileUploadConfig"
             transferMode="StreamedRequest" />
  </basicHttpBinding>
</bindings>

and a service endpoint on your service using that binding configuration: 以及使用该绑定配置的服务上的服务端点:

<services>
  <service name="FileUploadService">
     <endpoint name="UploadEndpoint"
               address="......."
               binding="basicHttpBinding"
               bindingConfiguration="FileUploadConfig"
               contract="IYourFileService" />
  </service>
</services>

and then, in your client, you need to open eg a filestream and send that to the service method without closing it. 然后,在您的客户端中,您需要打开例如文件流并将其发送到服务方法而不关闭它。

Hope that helps! 希望有所帮助!

Marc

您可以查看WCF流媒体功能。

In addition to increasing readerQuota settings (mentioned above) I had to also up the maxRequestLength inside the httpRuntime attribute. 除了增加readerQuota设置(如上所述)之外,我还必须在httpRuntime属性中增加maxRequestLength。

<system.web>
    <httpRuntime maxRequestLength="2097151" />
</system.web>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用WCF Web服务器将类等于实现发送给客户端 - How to send class equals implementation to client using a WCF Web Server 如何使用C#将对象从RESTful客户端发送到WCF RESTful服务器 - how to send an object from RESTful client to WCF RESTful server using C# 从服务器发送列表,但如何在客户端(WCF)C#中打印 - send a list from server, but how to print in the client (WCF) C# 从WCF服务器向Delphi客户端发送大数据 - Sending large data from WCF Server to Delphi Client 如何在不使用处理程序的情况下使用ajax从客户端将文件发送到服务器端? - How to send file to server side from client side using ajax without using handler? 如何使用WCF服务通过SOAP将大型Zip文件(50MB)传输到任何客户端? - How to transfer a large Zip file (50MB) using a WCF Service through SOAP to any client? 如何通过使用WCF NetHttpBinding(WebSockets)的服务器将一个客户端发送的消息广播(推送)到所有客户端? - How to broadcast (push) a message send by one client, to all clients through a server using WCF NetHttpBinding (WebSockets)? 无法通过 WCF 从客户端向服务器发送流数据 - Can't send streamed data from client to server via WCF 如何通过使用缓冲传输模式将大文件从Windows服务发送到WCF服务 - how to send large files to wcf service from windows service by using Buffered transfer mode 如何将图像从Android发送到WCF服务器? - How to send an image from Android to a WCF server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM