0

Alright, I managed to upload files to my FTP but now it's crying about certain folders not existing which makes sense. But I was wondering if there is a way to dynamically make those folders during the upload process? Any ideas?

This is the code I have 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 に答える