0

好吧,我设法将文件上传到我的 FTP,但现在它正在为某些不存在的文件夹而哭泣,这是有道理的。但我想知道是否有办法在上传过程中动态创建这些文件夹?有任何想法吗?

这是我有 ATM 的代码:

public static void UploadToFtp(String ftpServerAndPath, String fullPathToLocalFile, String username, String password)
    {
        // to the full URI.
        String filename = Path.GetFileName(fullPathToLocalFile);

        // Open a request using the full URI, c/file.ext
        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpServerAndPath + "/" + filename);

        // Configure the connection request
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(username, password);
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;

        // Create a stream from the file
        FileStream stream = File.OpenRead(fullPathToLocalFile);
        byte[] buffer = new byte[stream.Length];

        // Read the file into the a local stream
        stream.Read(buffer, 0, buffer.Length);

        // Close the local stream
        stream.Close();

        // Create a stream to the FTP server
        Stream reqStream = request.GetRequestStream();

        // Write the local stream to the FTP stream
        // 2 bytes at a time
        int offset = 0;
        int chunk = (buffer.Length > 2048) ? 2048 : buffer.Length;
        while (offset < buffer.Length)
        {
            reqStream.Write(buffer, offset, chunk);
            offset += chunk;
            chunk = (buffer.Length - offset < chunk) ? (buffer.Length - offset) : chunk;
        }
        // Close the stream to the FTP server
        reqStream.Close();
    }
4

1 回答 1

1

MSDN上的这篇文章

描述如何使用myFtpWebRequest.Method = WebRequestMethods.Ftp.MakeDirectory.

于 2012-11-25T20:58:42.587 回答