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
  • 27,041
  • 62
  • 151
  • 234

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
  • 44,639
  • 13
  • 95
  • 146
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,353
  • 14
  • 61
  • 106
0

Use this to but it into a byte array:

byte[] buffer = eftExport.ExportToBytes();

Now:

requestStream.Write(buffer, 0, buffer.Length);
RQDQ
  • 14,902
  • 2
  • 28
  • 55