简体   繁体   中英

Can't get file size when loading from WCF service host by web browser

I have some test WCF service with streamed webHttpBinding, which allows to download files by web browser. The problem is browser doesn't show file size and download progress. This is the service contract:

[ServiceContract]
public interface IDataTransferService
{
    [WebGet(UriTemplate = "download?file={fileName}")]
    Stream GetDownloadStream(string fileName);
}

This is the service implementation:

public sealed class DataTransferService : IDataTransferService
{
    public Stream GetDownloadStream(string fileName)
    {
        var context = WebOperationContext.Current;
        var stream = File.OpenRead(fileName);
        WebOperationContext.Current.OutgoingResponse.Headers["Content-Disposition"] = $"attachment; filename={Path.GetFileName(fileName)}";
        WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
        WebOperationContext.Current.OutgoingResponse.Headers["Content-Length"] = stream.Length.ToString();
        return stream;
    }
}

And this is the service configuration:

  <system.serviceModel>
    <services>
      <service name="WcfStreamingTest.Server.DataTransferService">
        <endpoint contract="WcfStreamingTest.IDataTransferService"
                  address="http://localhost:8000/streamingtest/api/transfer"
                  binding="webHttpBinding"
                  bindingConfiguration="streamedWeb"
                  behaviorConfiguration="web"/>
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="streamedWeb" transferMode="Streamed" sendTimeout="00:15:00" />
      </webHttpBinding>
    </bindings>    
    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

Name of file shows correctly, but information about file size is missing.

尝试将 transferMode 更改为 StreamedResponse

<binding name="streamedWeb" transferMode="StreamedResponse" sendTimeout="00:15:00" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM