【问题标题】:WCF couldn't return file as StreamWCF 无法将文件作为流返回
【发布时间】:2018-10-24 21:19:21
【问题描述】:

我有一个返回文件的 WCF webService 方法:

在 IMainService 中:

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

在 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;      
    }

这是我的 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>

当我在浏览器中调用此函数时。它不会返回任何内容。通过使用下载管理器,我会看到这个方法会不断被触发并且......什么都没有。

我已经将这个 MemoryStream 写在一个文件上,我发现它不是空的。

我应该怎么做才能返回文件?

【问题讨论】:

    标签: c# web-services wcf stream


    【解决方案1】:

    感谢楼主的提示,我们需要重新定位内存流的位置。

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

    我做了一个关于如何使用 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;
            }
        }
    

    配置。

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

    结果。

    【讨论】:

    • 谢谢。我已经测试了你的代码,它可以工作,但它会为我的代码增加一些开销,我应该将我的 MemoryStream 写在一个文件上并再次读取它。我曾尝试将 MemoryStream 转换为 FileStream,但它似乎不起作用。
    • 我发现了问题。我比较了两个流。在 MemoryStream 中,Position 等于 Stream.Lengh,但在 FileStream 中位置为零。我已将 MemoryStream 中的位置设置为零并且它可以工作。请编辑您的答案,我会接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 2011-09-22
    • 2021-09-25
    • 1970-01-01
    相关资源
    最近更新 更多