日期:2014-05-18  浏览次数:20870 次

关于WCF上传下载文件的问题
我最近在做一个基于WCF的网络文件系统,在使用WCF上传下载文件有下面的一些问题:
访问采用wsHttpBinding绑定,可是不支持流,于是单独定义了一个basicHttpBinding的终结点,参考MSDN中流的例程,可以上传文件,包括2G以上的文件都可以,可是basicHttpBinding不支持会话,而且流模式的接口中只能有一个流参数,服务器如何知道客户端的文件相关信息,如文件名等内容信息?
于是我将流传输文件的方式改成了netTcpBinding绑定,因为netTcpBinding支持会话,可运行时出错,一查,原因在于在WCF中流和会话不可同时支持,netTcpBinding采用流和Buffered模式倒是可以。
如何使用WCF来进行文件上传下载呢?

------解决方案--------------------
netTcpBinding肯定不会出错,我自己写过的。另外所谓的是否支持会话和获取文件信息有何关系?当一个文件要上传时,请求对应的函数参数中给一个文件名即可,非常简单的事。
------解决方案--------------------
传文件建议用 stream
服务端配置
XML code
<bindings>
            <netTcpBinding>
        <binding name="customTcpBinding"   transferMode="Streamed"   maxReceivedMessageSize="904800000" receiveTimeout="01:30:00" >
          <!--缺省 -->
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName"/>
          </security>
        </binding>
            </netTcpBinding>
        </bindings>

    
    <behaviors>
      <serviceBehaviors>
    <behavior name="FileServer.ServicesBehavior">
     <serviceMetadata httpsGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="false" />
      <serviceCredentials>
        <issuedTokenAuthentication allowUntrustedRsaIssuers="true"></issuedTokenAuthentication>
        <clientCertificate>
          <authentication certificateValidationMode="None"/>
        </clientCertificate>
        <serviceCertificate findValue="MyServer" storeLocation="CurrentUser" x509FindType="FindBySubjectName" storeName="My"/>
        <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="FileServer.MyCustomValidator,FileServer"/>
      </serviceCredentials>
    </behavior>

------解决方案--------------------
说道流传输,默认的流传输是不支持断点续传的,我重写了文件流,让流传输支持断点续传。代码不难,有需要的吗?