[英]File upload on FTP server not working
我正在嘗試將文件上傳到我的ftp服務器。 不幸的是,上載方法似乎可行,但無法上載我選擇的文件。 請在下面查看我的代碼:
public void UploadFileToFTP(string source)
{
try
{
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("ftp://xxx.bplaced.net/Test");
ftp.Credentials = new NetworkCredential("BN", "PW");
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(source);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
MessageBox.Show("Upload successful!");
}
catch (Exception ex)
{
throw ex;
}
}
public void button_Upload_Click(object sender, RoutedEventArgs e)
{
string sourcefilepath = @"C:\Users\MyPC\Desktop\Test\Texts\New.html";
UploadFileToFTP(sourcefilepath);
}
請看下面的截圖:“是目錄”是什么意思?
錯誤明確: 問題已解決。
您應該調用GetResponse()方法來實際發送ftp請求。 您僅准備了要在代碼中發送的請求。
引用MSDN,
GetResponse導致建立控件連接,並且還可能創建數據連接。 GetResponse阻塞,直到收到響應為止。 為避免這種情況,您可以通過調用BeginGetResponse和EndGetResponse方法代替GetResponse來異步執行此操作。
所以,寫完文件內容后,調用
ftp.GetResponse()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.