【问题标题】:Writing a file from StreamReader stream从 StreamReader 流写入文件
【发布时间】:2012-04-06 22:51:41
【问题描述】:

我目前正在尝试从 WCF 下载音轨,我需要一些帮助将其写入硬盘,如何配置 streamwriter 或其他以在 webApp 中执行此操作?

// Use Service to download stream (returns System.IO.Stream) 
Stream stream = MyService.Download(("1231"));
// ReadStream
StreamReader reader = new StreamReader(stream);
reader.ReadToEnd();

// Write this stream to a file/Hard disk
???

【问题讨论】:

  • StreamReader 用于文本数据
  • woops -_- 在此之前只处理流中的文本,谢谢。

标签: c# asp.net wcf stream


【解决方案1】:
Stream stream = MyService.Download(("1231"));
using (Stream s = File.Create(path))
{
    stream.CopyTo(s);
}

【讨论】:

  • 应该这样做,但它不适用于他的代码,除非他使用 StreamReader 以外的东西或类似var reader = new StreamReader(stream, Encoding.GetEncoding(28591));
  • 你知道如何在 .Net 3.5 中做同样的事情吗?
  • @Fayilt 有一个循环。 void CopyTo(Stream from, Stream to) { byte[] buffer = new byte[0x10000]; int read = from.Read(buffer, 0, buffer.Length); while (read > 0) { to.Write(buffer, 0, read); read = from.Read(buffer, 0, buffer.Length); } }
  • wth??他要求的是 StreamReader 类而不是 Stream。
猜你喜欢
  • 2015-11-24
  • 1970-01-01
  • 2013-12-28
  • 2019-02-13
  • 1970-01-01
  • 2013-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多