简体   繁体   English

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

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

I'm creating a CSV file from a List<> like this: 我正在从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();
}

I want to send this CSV to server via FTP in same action (maybe in same method). 我想以相同的操作(也许以相同的方法)通过FTP将CSV发送到服务器。 How can i handle this CSV and upload to server via FTP in C#? 如何处理该CSV并通过C#通过FTP上传到服务器?

I've used the WebClient before without issue. 我之前使用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); 

Here you go for uploading to an FTP server (taken from Microsoft's site) 在这里,您可以上传到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();
            }
        }
    }
}

I putted StringWriter result into MemoryStream and sent it to UploadFile method and now it's ok. 我将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