简体   繁体   中英

WCF couldn't return file as Stream

I have a WCF webService method which will returns files:

In IMainService:

    [OperationContract]
    [WebInvoke(Method = "GET",
               RequestFormat = WebMessageFormat.Json,
               BodyStyle = WebMessageBodyStyle.Wrapped,
               UriTemplate = "/PerformScan")]
    Stream PerformScan();

In MainService.cs:

    public Stream PerformScan()
    {
         MemoryStream SomeStream = new MemoryStream();

         // Filling this memory stream using some processes on the data

         WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
         WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-disposition", "inline; filename=Scan.Tiff");

         return SomeStream;      
    }

and Here is my App.Config:

<system.serviceModel>
  <services>
    <service name="ShamsScanner.WCF.ScanService" behaviorConfiguration="mexBehavior">
      <endpoint address="" binding="webHttpBinding" contract="ShamsScanner.WCF.IScanService" behaviorConfiguration="web"/>
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:1369"/>
          <add baseAddress="net.tcp://localhost:1369"/>
        </baseAddresses>
      </host>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="mexBehavior">
        <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
        <serviceDebug includeExceptionDetailInFaults="False"/>
      </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
      <behavior name="web">
        <webHttp/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
</system.serviceModel>

When i call this function in my browser. it will returns nothing. By using a download manager i will see this method will constantly fired and ... nothing.

I have written this MemoryStream on a file and i have seen that its not empty.

What should i do to return a file ?

Thanks for the tips of the landlord, We need to re-target the position of the memory stream.

  Stream ms = new MemoryStream();   
        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
        fs.CopyTo(ms);
        ms.Position = 0;

I have made a demo about how to download a file with Filestream.

IService.

[OperationContract]
    [WebGet]
    Stream Download();

Service.cs

public Stream Download()
    {
        string file = @"C:\1.png";
        try
        {
            //MemoryStream ms = new MemoryStream();
            //Stream stream = File.OpenRead(file);
            FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read);
            //byte[] bytes = new byte[fs.Length];
            //fs.Read(bytes, 0, (int)fs.Length);
            //ms.Write(bytes, 0, (int)fs.Length);
            WebOperationContext.Current.OutgoingResponse.ContentType = "image/png";
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-disposition", "inline; filename=Scan.Tiff");
            return fs;
            //return Stream;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

Config.

<service name="WcfServiceFile.Service1">
    <endpoint address="" binding="webHttpBinding" contract="WcfServiceFile.IService1" behaviorConfiguration="beh">
    </endpoint>
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="beh">
      <webHttp />
    </behavior>
  </endpointBehaviors>

Result. 在此处输入图片说明

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