C# 使用WCF Restful服务的PDF
任务:能够在用户单击链接时呈现PDF(这是一个WCF服务{example:})。该服务从SSRS服务器获取报告并返回流。下面是中的一段代码C# 使用WCF Restful服务的PDF,c#,wcf,pdf,rest,C#,Wcf,Pdf,Rest,任务:能够在用户单击链接时呈现PDF(这是一个WCF服务{example:})。该服务从SSRS服务器获取报告并返回流。下面是中的一段代码 [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)] public class AssistReportServices : IAssistReportServices { public Stream GetReport(int value) { //skippe
[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);
到目前为止还不错。但问题是。。。单击上面的链接时,在标题为Adobe Reader和消息的对话框中出现以下错误:
文件不是以“%PDF-”开头的
错误图像:
我可以将内存流保存到一个文件中,然后手动打开pdf,它可以正常打开,没有问题
谢谢 如何将其发送到浏览器?它看起来是用错误的mime类型发送的。它应该作为
Response.ContentType=“application/pdf”输入代码>我发现哪里出了问题。我使用了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”具有路径
名为“value”的变量没有“string”类型。变量
对于UriTemplate,路径段的类型必须为“string”
我通过将操作参数的数据类型更改为字符串来解决这个问题
多亏了Mrchief,我使用他的输入添加了以下代码行,以帮助我显示保存/取消对话框:-
WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-disposition", "inline; filename=apurvReport.pdf");
更新了这个问题,web.config文件的键值对是:是的,但是你在哪里将它写入响应流?服务是REST-ful的,所以我应该能够将链接作为锚定标记嵌入到我的HTML页面中。我的服务应将响应流式传输回浏览器,浏览器应提示打开“保存”对话框。如果我的理解不正确,请原谅。然后尝试将此标题添加到响应:Content-disposition:attachment;filename=fname.ext
谢谢。我仍然无法获得错误消息中描述的结果。Acrobat认为打开名为012
的文件而不是名为012.pdf
的文件更像是一个问题。请不要忘记将此标记为答案-回答您自己的问题是有效的。谢谢。直到24小时才允许我标记答案。可能是因为我的用户级别。