【问题标题】:PDF using WCF Restful Services使用 WCF Restful 服务的 PDF
【发布时间】:2011-07-25 16:54:33
【问题描述】:

任务:当用户单击链接时能够呈现 PDF(这是 WCF 服务{示例:http://localhost:6186/MyReportServices.svc/reports/012})。服务从 SSRS 服务器获取报告并返回流。这是 中的一段代码。

[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class AssistReportServices : IAssistReportServices
{
    public Stream GetReport(int value)
    {
      //skipped some lines of code. 
      try
        {
          result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
          execInfo = rs.GetExecutionInfo();             
        }
      catch (SoapException err)
        {
            throw;
        }
      MemoryStream ms = new MemoryStream();
      ms.Write(result, 0, result.Length);
      ms.Position = 0;
      //WebOperationContext.Current.OutgoingResponse.ContentType = ConfigurationManager.AppSettings["ResponseType"];
      WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
      return ms; 
    }
 }

我的运营合同如下:

[OperationContract(Action = "*")]
[WebInvoke(Method = "POST",
    UriTemplate = "reports/{value}")]
Stream GetReport(int value);

到目前为止一切顺利。但问题出在这里...当我单击上面的链接时,我在标题为 Adob​​e Reader 和消息的对话框中收到以下错误:

文件不以“%PDF-”开头。

错误图片:http://i.imgur.com/A4J68.png

我可以将内存流保存到一个文件并手动打开 pdf,它可以正常打开而没有问题。

谢谢。

【问题讨论】:

  • 错误消息看起来更像是一个问题,Acrobat 认为它正在打开一个名为 012 而不是名为 012.pdf 等的文件。

标签: c# wcf pdf rest


【解决方案1】:

我发现我哪里出错了。我曾使用 Web.Config 文件来配置我的 WCF 服务。当我使用以下代码切换到基于代码的配置时,该服务工作并指出了我的错误。

static void Main(string[] args)
{

    ServiceHost serviceHost = new ServiceHost(typeof(MyReportingServices.MyReportServices), ServiceEndpointUri);
    WebHttpBinding binding = new WebHttpBinding();
    ServiceEndpoint sep = serviceHost.AddServiceEndpoint(typeof(MyReportingServices.IMyReportServices), binding, string.Empty);
    sep.Behaviors.Add(new WebHttpBehavior());
    serviceHost.Open();
    Console.WriteLine("Service is running @ " + ServiceEndpointUri);
    Console.WriteLine();
    Console.WriteLine("Press enter to shutdown service.");
    Console.ReadLine();
    serviceHost.Close();
}

错误是:

合约“IMyReportServices”中的操作“GetReport”有一个路径 名为“值”的变量不具有“字符串”类型。变量 对于 UriTemplate 路径段必须具有类型“字符串”。

我通过将操作参数的数据类型更改为字符串来解决这个问题。

感谢 Mrchief,因为我使用他的输入添加了以下代码行,以帮助我显示保存/取消对话框:-

WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-disposition", "inline; filename=apurvReport.pdf"); 

【讨论】:

  • 别忘了将此标记为答案 - 回答您自己的问题是有效的。
  • 谢谢。直到 24 小时才允许我标记答案。可能是因为我的用户级别。
【解决方案2】:

您如何将其发送到浏览器?看起来它是用错误的 mime 类型发送的。它应该输入为Response.ContentType = "application/pdf";

【讨论】:

  • 更新问题,web.config 文件的键值对为:
  • 是的,但是你在哪里将它写入响应流?
  • 该服务是 REST-ful,所以我应该能够将链接 localhost:6186/MyReportServices.svc/reports/012 作为锚标记嵌入到我的 HTML 页面中。我的服务应将响应流式传输回浏览器,并且浏览器应提示打开保存对话框。如果我的理解不正确,请见谅。
  • 然后尝试将此标头添加到响应中:Content-disposition: attachment; filename=fname.ext
  • 谢谢。我仍然无法达到social.msdn.microsoft.com/Forums/en/wcf/thread/… 中所述的结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-10
  • 1970-01-01
  • 2016-01-11
  • 2013-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多