asp.net web api:访问上传的文件流

时间:2012-08-09 10:53:16

标签: asp.net asp.net-web-api

我有一个asp.net web api应用程序,它充当wcf Web服务的中继。在某些情况下,我想上传大文件。 wcf服务中的方法接受文件作为流。  我不想在我的中间服务器上保存文件我想访问上传文件的流并将其提供给wcf方法,以便将数据直接传输到wcf服务。

以下是客户端下载文件时的类似情况

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

1 个答案:

答案 0 :(得分: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);
        }
相关问题