2012-08-09 15 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); 
     }