I am working on an application where I need to upload CSV files to an FTP server. I am facing an error mentioned below.
can not access the closed stream
Here is a code sample
public Stream WriteCSV<T>(List<T> content) where T : class
{
var stream = new MemoryStream();
using (StreamWriter writer = new StreamWriter(stream))
{
using (CsvWriter csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(content);
writer.Flush();
}
}
return stream;
}
// calling method
var _writeService = new WriteService();
using var stream = _writerService.WriteCSV(partLocatorFiltered.ToList());
var fileUploadPath = $"/input/partslocator042/KE5206205_TEST_CSV.csv";
await _iFtpClientService.SendFileAsync(_options?.FTP, stream, fileUploadPath);
I am facing an issue while uploading the CSV stream to the FTP server.