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

本文关键字:文件 FTP 服务器 CSV 一个 MVC 创建 | 更新日期: 2023-09-27 18:17:48

我正在从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和上传到服务器通过FTP在c# ?

在MVC / c#中创建一个CSV文件并通过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;
        }
    ...