Search code examples
silverlightfilewcf-ria-services

Write csv file at server-side, and send it to silverlight client via WCF RIA Services


I have created csv file at server-side. Now I want to send it to my silverlight client via WCF RIA Services. I created the following:

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

  ???
}

Should my service send byte array? If then how to get byte array from StreamWriter?


Solution

  • First of, the StreamWriter class if for writing to a file. As long as i understand your question you want to read from the File.

    The easiest way to read all bytes from a file is to use the File class and the ReadAllBytes method.

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

    The other way is to use the StreamReader class, to read blocks of bytes from the file and to define additional encodings for reading.