C# 从WCF REST服务自动下载pdf
我有一个WCF REST服务,它从客户端获取一个id,然后下载一个文件。我似乎在回复的正文中得到了返回的文件,但它不是自动下载的 我以前从未尝试过这样做,所以我想知道是否有人能提供一些指导。我正在将流返回到客户端 这是我的C# 从WCF REST服务自动下载pdf,c#,wcf,rest,download,C#,Wcf,Rest,Download,我有一个WCF REST服务,它从客户端获取一个id,然后下载一个文件。我似乎在回复的正文中得到了返回的文件,但它不是自动下载的 我以前从未尝试过这样做,所以我想知道是否有人能提供一些指导。我正在将流返回到客户端 这是我的运营合同: [OperationContract] [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, UriTemplate = "/GetFile/{id}")] Stream
运营合同
:
[OperationContract]
[WebInvoke(Method = "GET",
RequestFormat = WebMessageFormat.Json,
UriTemplate = "/GetFile/{id}")]
Stream GetFile(string id);
这是我的GetFile
方法:
public Stream GetFile(string BillingPeriodId)
{
byte[] bytes = File.ReadAllBytes(@"C:\pdf-test.pdf");
WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Disposition", "attachment;inline; filename=pdf-test.pdf");
return new MemoryStream(bytes);
}
再一次,我的服务似乎返回了
200
。有人能帮忙吗?不要从您的服务中返回内存流。即使它看起来很好,而且编译得很好,它也不起作用。从服务返回流对象
您的代码可以如下所示:
[WebInvoke(Method = "GET", UriTemplate = "GetFile/{BillingPeriodId}", RequestFormat = WebMessageFormat.Json)]
public Stream GetFile(string BillingPeriodId)
{
WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Disposition", "attachment;inline; filename=pdf-test.
Stream stream = File.OpenRead(@"C:\pdf-test.pdf");
return stream;
}
如果要返回流,为什么WebInvoke属性中有ResponseFormat=WebMessageFormat.Json?@fanuc_bob-旧代码。。。从那时起我就把它拿走了,但那不是我的问题。