2

I'm truing to send a CSV with csvhelper. But when I send it, it is always empty.

using (var sftProvider =new SFTProvider(Configuration))
using (var csvProvider=new CSVProvider(Configuration))
using (Stream fileStream = new MemoryStream())
{
    var sftp = sftProvider.SFTPConnection("", "", "");
    var nameFile = "/ethias/"+DateTime.Now.ToString(DateFormatConstants.DATE_FORMAT) + ".csv";
    if (sftp.Exists(nameFile))
    {
        sftProvider.ReadFileSFTP(sftp, nameFile, fileStream);
    } 
    await csvProvider.WriteCsv(new List<EthiasResultDTO>() {ethiasResultDto},fileStream); 
    sftProvider.UploadFileSFTP(sftp,nameFile,fileStream);
}

The code for the csv:

public async Task WriteCsv<T>(List<T> csvInfo, Stream stream)
{
    using(var writer = new StreamWriter(stream, Encoding.UTF8))
    using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
    {
        csv.Configuration.Delimiter = ";";

        if ( stream.Length > 0)
        {
            csv.Configuration.HasHeaderRecord = false;
        }
        csv.WriteRecords(csvInfo);
    }
}

i try serveral things but it always empty file that is create .

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
20 ans IPl
  • 41
  • 4
  • 3
    Into the same fileStream, you first apply ReadFileSFTP(), then WriteCsv() . I think what you do is to get a file from an FTP location, append some records to it, then to put it back. If this is the case, just set `fileStream.Position = 0;`before passing it to `UploadFileSFTP` – Oguz Ozgul Mar 15 '20 at 10:23
  • 3
    If the suggestion by @OguzOzgul does not help, we will need [mcve]. You haven't told us anything about the FTP/SFTP library. It's not even clear if you use FTP or SFTP. – Martin Prikryl Mar 15 '20 at 10:26
  • i use sftp Renci.SshNet – 20 ans IPl Mar 15 '20 at 11:22
  • i only have a generique class for sftp – 20 ans IPl Mar 15 '20 at 11:22
  • i only do the recovery of the record if there a file but i have the problem also when i only create and ad a new file the record in the csv is empty – 20 ans IPl Mar 15 '20 at 11:24
  • System.ObjectDisposedException: Cannot access a closed Stream. i have this error when i use using in my methode writecsv – 20 ans IPl Mar 15 '20 at 11:28
  • I think you need something similar to the answer to this question. https://stackoverflow.com/questions/21093150/using-csvhelper-to-output-stream-to-browser – David Specht Mar 15 '20 at 12:15

1 Answers1

0

You have to seek the read pointer of the stream back to beginning after writing to it. This is covered for example in these questions:
Upload data from memory to SFTP server using SSH.NET
Upload from ByteArray/MemoryStream using SSH.NET - File gets created with size 0KB


For that you also have to make sure that the StreamWriter does not close the memory stream:
Is there any way to close a StreamWriter without closing its BaseStream?


using (var writer = new StreamWriter(stream, Encoding.UTF8, 1024, true))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
    // Write
}

stream.Position = 0;
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992