簡體   English   中英

在MVC / C#中創建CSV文件並通過FTP上傳到服務器

[英]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服務器(取自Microsoft網站)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM