【问题标题】:Write csv file at server-side, and send it to silverlight client via WCF RIA Services在服务器端写入 csv 文件,并通过 WCF RIA 服务发送到 silverlight 客户端
【发布时间】:2012-10-15 14:16:49
【问题描述】:

我在服务器端创建了 csv 文件。现在我想把它发送给我的 silverlight 客户 通过 WCF RIA 服务。我创建了以下内容:

public byte[] GetMyCSV()
{
  string file = @"c:\test.csv";
  using (StreamWriter sw = new StreamWriter(file, true, Encoding.GetEncoding(932)))
  {
    //write csv file content
    ...
  }

  ???
}

我的服务应该发送字节数组吗?那么如何从 StreamWriter 中获取字节数组?

【问题讨论】:

    标签: silverlight file wcf-ria-services


    【解决方案1】:

    首先,StreamWriter 类用于写入文件。只要我理解您想从文件中读取的问题。

    从文件中读取所有字节的最简单方法是使用File 类和ReadAllBytes 方法。

    public byte[] GetMyCSV(){
      string file = @"c:\test.csv";
      bytes[] fileBytes;
    
      using (var fileStream = File.OpenRead(file))  {
        fileBytes = fileStream.ReadAllBytes();
      }
    
      return fileBytes;
    }
    

    另一种方法是使用StreamReader 类,从文件中读取字节块并定义额外的读取编码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 2011-05-29
      相关资源
      最近更新 更多