2012-08-09 16 views
0

我有一個asp.net web api應用程序,它充當一個wcf web服務的中繼。在某些情況下,我想上傳大文件。 wcf服務中的方法接受文件爲流。 我不想保存我的中間服務器上的文件我想訪問上傳文件的流並將其提供給wcf方法,以便將數據直接傳輸到wcf服務。asp.net web api:訪問上傳的文件流

下面是類似的情況時,客戶端下載文件

using (IProductsChannel channel = ChannelFactory.CreateChannel()) 
      { 
       result.Content = new StreamContent(channel.GetFile()); 
       result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); 
       return result; 
      } 

回答

0

這裏是做這件事的至少一種方式。使用HTTPContext這個問題唯一的問題是它不適合單元測試,所以我們必須在最終解決方案中抽象出它。

var file = HttpContext.Current.Request.Files[0]; 


      var result = new HttpResponseMessage(HttpStatusCode.OK); 

      using (IProductsChannel channel = ChannelFactory.CreateChannel()) 
      { 
       channel.SaveFile(file.InputStream); 
      } 

      return new HttpResponseMessage(HttpStatusCode.Created); 
     }