2

I'm creating a CSV file from a List<> like this:

public void ExportListToCSV()
{
    StringWriter sw = new StringWriter();

    sw.WriteLine("\"username\",firstname, lastname , email , course1 , course2 , course3 , course4 ");

    Response.ClearContent();
    Response.AddHeader("content-disposition", "attachment;filename=OgrenciListesi.csv");
    Response.ContentType = "text/csv";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1254");
    Response.Charset = "windows-1254";

    foreach (var line in Liste())
    {
        sw.WriteLine(string.Format("\"{0}\", {1} , {2} , {3} , {4} , {5} , {6} , {7} ",
                                    line.KullaniciKodu,
                                    line.Adi,
                                    line.Soyadi,
                                    line.Email,
                                    line.Course1,
                                    line.Course2,
                                    line.Course3,
                                    line.Course4
                                    ));
    }

    Response.Write(sw.ToString());
    Response.End();
}

I want to send this CSV to server via FTP in same action (maybe in same method). How can i handle this CSV and upload to server via FTP in C#?

3
  • First save it to a file instead of sending it to the client then use FtpWebRequest to upload it to FTP server. Have a look at this How to: Upload Files with FTP Commented Oct 15, 2015 at 14:43
  • Did you mean save to disk or memory? (Diske kaydetmeden yollamak istiyorum da) Commented Oct 15, 2015 at 14:59
  • :-) In that case try WebClient.UploadData to upload a stream without saving to a file Commented Oct 15, 2015 at 15:09

3 Answers 3

2

I've used the WebClient before without issue.

   WebClient client = new WebClient();
   client.Credentials = new NetworkCredential("username", "password");

   string address= @"ftp://example.com/";
   string filename= @"C:\foo.csv"

   client.UploadFile(address, filename); 
Sign up to request clarification or add additional context in comments.

1 Comment

But i have no such a file on disk to send. I want to create it in memory and send to FTP.
2

Here you go for uploading to an FTP server (taken from Microsoft's site)

 using System;
 using System.IO;
 using System.Net;
 using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main ()
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential ("anonymous","[email protected]");

            // Copy the contents of the file to the request stream.
            StreamReader sourceStream = new StreamReader("testfile.txt");
            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();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

            response.Close();
            }
        }
    }
}

Comments

0

I putted StringWriter result into MemoryStream and sent it to UploadFile method and now it's ok.

    ...

        try
        {
        var data = UnicodeEncoding.ASCII.GetBytes(sw.ToString());
        using (Stream stream = new MemoryStream(data))
        {
            stream.Position = 0;
            bool res = this.UploadFile(stream, "OgrenciListesi.csv", "tmp");
        }
        }
        catch (Exception)
        {
            throw;
        }
    ...

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.