【问题标题】:How to upload a folder through FTP?如何通过FTP上传文件夹?
【发布时间】:2012-10-27 01:38:18
【问题描述】:

我正在尝试通过 FTP 上传文件夹(及其内容),但当我尝试上传时,我不断收到错误消息,提示未选择文件。

你知道有什么方法可以上传文件夹及其内容吗?

这是我正在使用的代码。 (我要上传Plugins文件夹下的所有文件):

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);
    }
}

【问题讨论】:

    标签: c# windows forms upload ftp


    【解决方案1】:

    您的 URI 不正确,需要在 URI 中指定文件名。

    // Create FtpWebRequest object from the Uri provided
        System.Net.FtpWebRequest _FtpWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(new Uri(_UploadPath + "/" + _FileInfo.Name));
    

    另外,请考虑为您的流使用“using”关键字,以便在出现异常时正确处理它们。

    using (System.IO.FileStream _FileStream = _FileInfo.OpenRead())
                    {
    
                        // Stream to which the file to be upload is written
    
                        using (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);
                            }
    
                            //No need to Close the file stream and the Request Stream
    
                        }
                    }
    

    【讨论】:

    • 我把你创建的第一行代码放在哪里?您不调用子 UploadFile。
    • 还是不能上传文件夹
    • 我只是在您的 UploadFile 方法中编辑了代码。如果要上传文件夹,则必须循环遍历文件夹中的文件并事先使用 Ftp.MakeDirectory 方法。无论哪种方式,这可能都不是您想要上传文件的方式。您应该将您的方法修改为异步的。
    猜你喜欢
    • 2012-02-22
    • 2011-05-07
    • 2013-09-26
    • 2012-04-26
    • 2013-03-24
    • 2017-03-23
    • 1970-01-01
    • 2011-03-30
    相关资源
    最近更新 更多