簡體   English   中英

使用 WCF Rest 服務下載文件?

[英]Download file using WCF Rest service?

如果有辦法通過流使用rest上傳文件,還會有“ Download ”嗎? 如果是的話,你能告訴我怎么做嗎? 提前致謝!

我用來從我的 REST 服務下載文件的示例方法:

[WebGet(UriTemplate = "file/{id}")]
        public Stream GetPdfFile(string id)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/txt";
            FileStream f = new FileStream("C:\\Test.txt", FileMode.Open);
            int length = (int)f.Length;
            WebOperationContext.Current.OutgoingResponse.ContentLength = length;
            byte[] buffer = new byte[length];
            int sum = 0;
            int count;
            while((count = f.Read(buffer, sum , length - sum)) > 0 )
            {
                sum += count;
            }
            f.Close();
            return new MemoryStream(buffer); 
        }

您還可以使用以下

 public Stream GetFile(string id)
 {
      WebOperationContext.Current.OutgoingResponse.ContentType = "application/txt";
      var byt = File.ReadAllBytes("C:\\Test.txt");
      WebOperationContext.Current.OutgoingResponse.ContentLength = byt.Length;
      return new MemoryStream(byt);
 }

當它被定義為

[WebGet(UriTemplate = "file/{id}")]
[OperationContract]
Stream GetFile(string id);

暫無
暫無

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

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