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 .

7
  • 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 Commented Mar 15, 2020 at 10:23
  • 3
    If the suggestion by @OguzOzgul does not help, we will need minimal reproducible example. You haven't told us anything about the FTP/SFTP library. It's not even clear if you use FTP or SFTP. Commented Mar 15, 2020 at 10:26
  • i use sftp Renci.SshNet Commented Mar 15, 2020 at 11:22
  • i only have a generique class for sftp Commented Mar 15, 2020 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 Commented Mar 15, 2020 at 11:24

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.