在 MVC / C# 中通过 FTP 创建 CSV 文件并上传到服务器

Create a CSV File and Upload to Server via FTP in MVC / C#

我正在从 List<> 创建 CSV 文件,如下所示:

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();
}

我想通过 FTP 以相同的操作(可能以相同的方法)将此 CSV 发送到服务器。我如何处理此 CSV 并通过 C# 中的 FTP 上传到服务器?

我以前用过WebClient没有问题。

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

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

   client.UploadFile(address, filename); 

在这里你可以上传到 FTP 服务器(取自微软的网站)

 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","janeDoe@contoso.com");

            // 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();
            }
        }
    }
}

我把 StringWriter 的结果放到 MemoryStream 中,然后发送到 UploadFile 方法,现在可以了。

    ...

        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;
        }
    ...