2016年12月10日 星期六

[C#]上傳檔案至FTP(使用WebClient)

private void FTPUpload()
{
    string userName = "User";
    string password = "****";
    string uploadUrl = "ftp://(FTP Address):Port/test.txt";

    if (!System.IO.File.Exists(@"C:\Temp.txt"))
        return;

    // 要上傳的檔案
    StreamReader sourceStream = new StreamReader(@"C:\Temp.txt");
    byte[] data = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
    sourceStream.Close();

    WebClient wc = new WebClient();
    wc.Credentials = new NetworkCredential(userName, password);

    try
    {
        wc.UploadData(uploadUrl, data);
    }
    catch (Exception ex)
    {}
}

Reference Web:
MSDN:如何透過 FTP 上傳檔案
CODE-FTP上傳檔案的精簡寫法
C# 上傳文件至FTP
Asp.net - C# 從ftp上傳檔案~* (對此FTP命令而言,要求的URI無效問題)
Filezilla Server供外連的設定及C# FTP上傳實作
C# 上傳檔至遠端伺服器(適用于桌面程式及web程式)
C# FTP 上傳

沒有留言:

張貼留言