2015-10-15 102 views
1

我創建從一個List<>這樣一個CSV文件:創建一個CSV文件,並通過FTP在MVC/C#上傳到服務器

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發送到服務器(也許用相同的方法)。我如何處理這個C​​SV並通過FTP在C#中上傳到服務器?

+0

首先將它保存到一個文件中,而不是發送給客戶端,然後使用FtpWebRequest將其上傳到FTP服務器。看看這個[如何:用FTP上傳文件](https://msdn.microsoft.com/en-us/library/ms229715(v = vs.110).aspx) –

+0

您是否意味着保存到磁盤或記憶? (Diske kaydetmeden yollamak istiyorum da) – LacOniC

+0

:-)在這種情況下,嘗試[WebClient.UploadData](https://msdn.microsoft.com/en-us/library/tdbbwh0a(v = vs.110).aspx)上傳一個流沒有保存到一個文件 –

回答

2

我以前用過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); 
+0

但我沒有這樣的文件在磁盤上發送。我想在內存中創建它併發送到FTP。 – LacOniC

1

在這裏,你去上傳到我的推杆的StringWriter導致到的MemoryStream並將其發送到UploadFile方法的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","[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(); 
      } 
     } 
    } 
} 
0

,現在它的確定。

... 

     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; 
     } 
    ... 
相關問題