Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从WCF REST服务自动下载pdf_C#_Wcf_Rest_Download - Fatal编程技术网

C# 从WCF REST服务自动下载pdf

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

我有一个WCF REST服务,它从客户端获取一个id,然后下载一个文件。我似乎在回复的正文中得到了返回的文件,但它不是自动下载的

我以前从未尝试过这样做,所以我想知道是否有人能提供一些指导。我正在将流返回到客户端

这是我的
运营合同

[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-旧代码。。。从那时起我就把它拿走了,但那不是我的问题。