1

I am currently creating a CSV file and then I ftp that file. This is working fine. However, I dont want to save the csv file, i want to create it to memory and then ftp it.

This is my current code:

private void Csv()
    {             
        CsvExport eftExport = new CsvExport();
        eftExport.AddRow();
        eftExport["customer_reference"] = "Ref";           
        eftExport["landline"] = "01234567890";
        string url = "C:/Content/Cms/DD/";
        string fileName = "file.csv";
        eftExport.ExportToFile(url + fileName);
        this.FtpFile(url, fileName);
    }

    private void FtpFile(string url, string fileName)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://url.co.uk/" + fileName);
        request.Method = WebRequestMethods.Ftp.UploadFile;

        request.Credentials = new NetworkCredential ("Administrator", "pass");

        StreamReader sourceStream = new StreamReader(url + fileName);
        byte[] fileContents = Encoding.UTF8.GetBytes (sourceStream.ReadToEnd());
        sourceStream.Close();
        request.ContentLength = fileContents.Length;

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(fileContents, 0, fileContents.Length);
        requestStream.Close();                       
    }

but in stead of doing eftExport.ExportToFile(url + fileName); i dont want it to save to machine??

Beginner
  • 28,539
  • 63
  • 155
  • 235

3 Answers3

0

Use the ExportToBytes() function of your CsvExport class.

Then change your FtpFile() to accept a byte array and remove the stream reader

you should end up with quite a bit less code :)

Colin Pickard
  • 45,724
  • 13
  • 98
  • 148
0

If your CsvExport type has an ExportToStream or similar simply use that create the stream that you subsequently write to the requestStream.

Myles McDonnell
  • 12,943
  • 17
  • 66
  • 116
0

Use this to but it into a byte array:

byte[] buffer = eftExport.ExportToBytes();

Now:

requestStream.Write(buffer, 0, buffer.Length);
RQDQ
  • 15,461
  • 2
  • 32
  • 59