【问题标题】:asp.net web api: accessing uploaded file streamasp.net web api:访问上传的文件流
【发布时间】:2012-08-09 10:53:16
【问题描述】:

我有一个 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;
            }

【问题讨论】:

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


    【解决方案1】:

    这里至少有一种方法。使用 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);
            }
    

    【讨论】:

      猜你喜欢
      • 2020-07-10
      • 2014-05-15
      • 2016-11-14
      • 2021-07-07
      • 2012-07-27
      • 2019-10-10
      • 2019-02-10
      • 1970-01-01
      • 2021-08-09
      相关资源
      最近更新 更多