簡體   English   中英

從WCF REST服務自動下載pdf

[英]Automatically downloading pdf from WCF REST service

我有一個WCF REST服務,該服務從客戶端獲取ID,然后下載文件。 我似乎在響應的正文中返回了文件,但該文件沒有自動下載。

我以前從未嘗試過這樣做,所以我想知道是否有人可以提供一些指導。 我將流返回給客戶端。

這是我的OperationContract

[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 有人可以幫忙嗎?

不要從服務中返回內存流。 即使它看起來不錯並且編譯正確,也無法正常工作。 從服務中返回一個Stream對象。

您的代碼如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM