Flex FTP Make Directory

I’m messing around with Flex FTP at the moment and found that there is no way yet to create a directory on the FTP server. I had a quick look through the code on how the other FTP actions were implemented. Being surprised with the nice setup of the code I had quite easily added the “make directory” action and got it working properly. You can download my MakeDirInv class here: http://ansuz.nl/toys/randomcode/MakeDirInv.as
All you need to do after that is patch the FTPClient class by adding the following method.

ActionScript

  1. /**
  2. * Creates a directory on the remote host.
  3. *
  4. * @param newDirName The name of the new directory to create.
  5. * @param parentDir (Optional) The directory to create the new directory in.
  6. */
  7. public function makeDir(newDirName:String, parentDir:String = "/"):void {
  8. trace("FTPClient.makeDir(newDirName, parentDir)");
  9. invoke(new MakeDirInv(this, newDirName, parentDir));
  10. }

Once you’ve done that, you’re ready to create directories on the FTP server you’re connected to, see the example below.

ActionScript

  1. ftpClient.addEventListener(FTPEvent.CREATE_DIR, dirCreatedHandler);
  2. ftpClient.makeDir("myNewDir", "/someSubDir/");

PS: This isn’t fully tested yet, so do let me know if you find some bugs.

Links

2 thoughts on “Flex FTP Make Directory

  1. Hi, nice example, but i have some problems when i’m tring it to my app:

    1) At “MakeDirInv.as” str 38:
    override protected function startSequence():void
    {
    sendCommand( new FTPCommand(Commands.MKD, parentDir + newDirName) );
    }

    I have error massage about “MKD”

    2) When I add EventListener, i haven’t “dirCreatedHandler”

    client.addEventListener(FTPEvent.CREATE_DIR, dirCreatedHandler);

    can u show dirCreatedHandler funk?

    3) Can u add to blog full app, with can create folder no server? it will be perfect to dounload working example to understand how it works

    Thank U!

  2. Hi,

    Thanks for commenting! 🙂

    I think you have to add ‘public static const MKD:String = “MKD”;’ to the list of commands in pl.maliboo.ftp.Commands.
    In my ‘dirCreatedHandler’ method I just have a trace statement and a call to another method that is specific to my app, the method signature looks like this:
    ‘private function dirCreatedHandler(event:FTPEvent):void’

    Hope this helps,

    Wijnand.

Leave a Reply

Your email address will not be published. Required fields are marked *