2010-01-22 121 views
0

我想將多行文本框流式傳輸到FTP服務器上的文本文件中。有人能告訴我我哪裏可能會出問題嗎?將文本框上傳到ftp C上的文本文件#

private void btnSave_Click(object sender, EventArgs e) 
{ 
    UriBuilder b = new UriBuilder(); 
    b.Host = "ftp.myserver.com"; 
    b.UserName = "user"; 
    b.Password = "pass"; 
    b.Port = 21; 
    b.Path = "/myserver.com/directories/" + selected + ".txt"; 
    b.Scheme = Uri.UriSchemeFtp; 
    Uri g = b.Uri; 

    System.Net.FtpWebRequest c = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(g); 
    c.Method = System.Net.WebRequestMethods.Ftp.DownloadFile; 

    System.Net.FtpWebResponse d = (System.Net.FtpWebResponse)c.GetResponse(); 

    System.IO.Stream h = d.GetResponseStream; 
    System.IO.StreamWriter SW = new System.IO.StreamWriter(h); 
    String[] contents = textBox1.Lines.ToArray(); 
    for (int i = 0; i < contents.Length; i++) 
    { 
     SW.WriteLine(contents[i]); 
    } 



    h.Close(); 
    SW.Close(); 

    d.Close(); 
} 

我正的錯誤是這樣的線:

System.IO.StreamWriter SW =新System.IO.StreamWriter(H);

流不可寫。

任何想法?

回答

5

從FTP站點響應流數據網站你。你需要請求流...但你不會想要一個方法DownloadFile - 你沒有下載,你正在上傳,所以你想要的UploadFile方法。

此外:使用using塊這樣的:

  • 如果引發異常你不關閉任何東西。
  • 在UI線程上這樣做網絡訪問是一個壞主意;當FTP請求發生時,UI線程將會阻塞(所以整個UI將掛起)。改用後臺線程。
+0

當我切換到上傳文件I得到這一行一個錯誤: System.IO.Stream H = d.GetResponseStream; 無法將方法組「GetResponseStream」轉換爲非委託類型「System.IO.Stream」。你打算採用這種方法嗎? – David 2010-01-22 16:38:09

+0

我現在得到這行的錯誤:System.IO.StreamWriter SW = new System.IO.StreamWriter(h);流不可寫... – David 2010-01-22 16:45:33

+0

正如我在答案中所說的,你不應該打開響應流 - 你應該打開* request *流。 – 2010-01-22 17:03:42

5

要上傳文件,您需要使用FtpWebRequest類。

引用:

當使用的FtpWebRequest目的是 文件上載到服務器,則必須 寫的文件的內容通過調用 GetRequestStream方法或其 異步同行獲得的請求 流, BeginGetRequestStreamEndGetRequestStream方法。在發送請求之前,您必須先寫入 流並關閉 流。

有關上傳文件的示例(您可以更改爲寫入流內容,如示例中所示)see here

+0

我沒有選擇添加getrequest流 - 它不斷拋出我的錯誤。 – David 2010-01-22 16:58:15

+0

@大衛:「它不斷拋出我的錯誤」不是很精確......你能給出確切的錯誤信息嗎? – 2010-01-22 17:04:21

0

MSDN取出並稍微修改:

public static bool UploadFileOnServer(string fileName, Uri serverUri) 
{ 
    // The URI described by serverUri should use the ftp:// scheme. 
    // It contains the name of the file on the server. 
    // Example: ftp://contoso.com/someFile.txt. 
    // The fileName parameter identifies the file 
    // to be uploaded to the server. 

    if (serverUri.Scheme != Uri.UriSchemeFtp) 
    { 
     return false; 
    } 
    // Get the object used to communicate with the server. 
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri); 
    request.Method = WebRequestMethods.Ftp.UploadFile; 

    StreamReader sourceStream = new StreamReader(fileName); 
    byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); 
    sourceStream.Close(); 
    request.ContentLength = fileContents.Length; 

    // This example assumes the FTP site uses anonymous logon. 
    request.Credentials = new NetworkCredential ("anonymous","[email protected]"); 
    Stream requestStream = request.GetRequestStream(); 
    requestStream.Write(fileContents, 0, fileContents.Length); 
    requestStream.Close(); 
    FtpWebResponse response = (FtpWebResponse) request.GetResponse(); 

    Console.WriteLine("Upload status: {0}",response.StatusDescription); 

    response.Close(); 
    return true; 
}