2

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.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
ds1709
  • 189
  • 3
  • 11

1 Answers1

0

Try to change transferMode to StreamedResponse

<binding name="streamedWeb" transferMode="StreamedResponse" sendTimeout="00:15:00" />
selami
  • 2,478
  • 1
  • 21
  • 25