-3
我想通過FTP上傳一個文件夾(及其內容),但是當我嘗試這樣做時,我總是收到一條錯誤消息,說沒有選中文件。如何通過FTP上傳文件夾?
你知道有什麼辦法讓它上傳文件夾及其內容嗎?
這是我使用的代碼。 (我想所有的上傳文件的文件夾插件中):
UploadFile(@"C:\\MainPlugins\\Plugins", "ftp://testsiteurl.com/public_html/wp-content/plugins/test plugin", "username", "password");
這是我使用的子:
public void UploadFile(string _FileName, string _UploadPath, string _FTPUser, string _FTPPass)
{
System.IO.FileInfo _FileInfo = new System.IO.FileInfo(_FileName);
// Create FtpWebRequest object from the Uri provided
System.Net.FtpWebRequest _FtpWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(new Uri(_UploadPath));
// Provide the WebPermission Credintials
_FtpWebRequest.Credentials = new System.Net.NetworkCredential(_FTPUser, _FTPPass);
// By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
_FtpWebRequest.KeepAlive = false;
// set timeout for 20 seconds
_FtpWebRequest.Timeout = 20000;
// Specify the command to be executed.
_FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
// Specify the data transfer type.
_FtpWebRequest.UseBinary = true;
// Notify the server about the size of the uploaded file
_FtpWebRequest.ContentLength = _FileInfo.Length;
// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
System.IO.FileStream _FileStream = _FileInfo.OpenRead();
try
{
// Stream to which the file to be upload is written
System.IO.Stream _Stream = _FtpWebRequest.GetRequestStream();
// Read from the file stream 2kb at a time
int contentLen = _FileStream.Read(buff, 0, buffLength);
// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
_Stream.Write(buff, 0, contentLen);
contentLen = _FileStream.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
_Stream.Close();
_Stream.Dispose();
_FileStream.Close();
_FileStream.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
我在哪裏放置您創建的第一行代碼?您不要調用子UploadFile。 –
我仍然不能上傳文件夾 –
我只是在UploadFile方法中編輯了代碼。 如果你想上傳一個文件夾,你必須循環遍歷循環中的文件夾中的文件,並事先使用Ftp.MakeDirectory方法。 無論哪種方式,這可能不是你想要如何上傳文件。你應該修改你的方法是異步的。 – Mataniko