Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / MFC
Article

EasyFtp 1.3.2 (for Applications)

Rate me:
Please Sign up or sign in to vote.
4.91/5 (28 votes)
21 Mar 2004CC (ASA 2.5)7 min read 149.4K   3.1K   119   40
A 'drop-in' FTP solution for applications providing a full GUI, extended commandline options and no resource files. Use standalone or compiled into your own app.

Image 1

Introduction

I remember when I first started using the internet (in 1993 using Winsock on a 14400 USR sportster modem) about just how exciting it was to no longer feel isolated on my computer. Nevertheless, it has still taken me 10 years or so to actually start programming for it - no-one ever said I ran before I could walk! And it has really only come about as a result of writing ToDoList, an XML 'based' task-list-thingy, also posted on CodeProject.

I had decided on XML as the data format precisely because it could be uploaded to my web site and processed using JavaScript, to say nothing of XSL transforms and the myriad of opportunities this presents for reporting.

And yet how was I still doing this uploading on a day-today basis? Using an FTP utility, that's how.

"And what's wrong with that", you may (or you may not) ask?

Nothing, from a strictly functional perspective, it's just that the process often strikes me as all so disjointed: saving a document in one app and then starting up a separate utility to transfer the file just screams at me for an overhaul.

What I really desired was to be able to simply call something like GetFile() (or run a standalone app), optionally with no parameters, and have it present all the necessary GUI required to allow me to get any file from anywhere and in the manner to which I've become accustomed over the last 8-9 years, namely via something like the standard Open/Save dialogs.

And that is what I've tried to do here.

EasyFtp is a no-brainer utility (no disrespect to anyone out there without a brain) which simply wraps a bunch of classes which do all the work.

Note: The reason I've wrapped it as an EXE is to allow it to be supported by TodoList's 'Tool' interface. It can just as easily be used by dropping the classes directly into your own application as I will explain later.

Requirements

I've already hinted at some of the requirements but here's a more exhaustive list:

  • Visually straightforward and familiar.
  • No configuration editing.
  • Seamless.
  • No RC file resources (especially if it's meant to be leveraged into multiple apps).
  • Totally free (always).
  • Lightweight (mind you, it's MFC - all things are relative).
  • Configurable - i.e. I can pass as much or as little information and the rest will be requested of me.

Design

The basic workflows that I envisaged were these:

  • Uploading a file

    1. Show standard 'Open' dialog to elicit the local file to be uploaded.
    2. Show 'Server Details' dialog to retrieve server name, username and password.
    3. Log on to server and display remote 'Save As' dialog to retrieve the remote file path to upload to.
    4. Display a progress/cancel dialog during the upload.

    Note: These numbers correspond directly to the image numbers above.

  • Downloading a file

    1. Show 'Server Details' dialog to retrieve server name, username and password.
    2. Log on to server and display remote 'Open' dialog to retrieve the remote file path to download.
    3. Show standard 'Save As' dialog to elicit the local file to be saved to.
    4. Display a progress/cancel dialog during the download

Sounds simple doesn't it?

And the beauty of it was that it was just as simple as it appears, taking into account (as I always do) that I had already written a lot of the code necessary to make it work for other projects.

And the particular code in question is CRuntimeDlg which I first presented in ToDoList as a means of constructing dialog boxes without the use of the RC editor and the resultant dependency on RC based dialog templates.

Note: If you're not clear on why this is such a significant issue, consider what has to happen if you want to reuse code that relies on dialog resources:

  • Copy/Include the .h/.cpp files.
  • Copy the dialog resources from the .RC file.
  • Verify that no nasty resource.h issues have arisen as a result of Visual Studio doing whatever it wants to ensure it can merge in the new resources.

Instead, CRuntimeDlg will allow you embed dialog control definitions within the dialog's .cpp file without any other fiddling about.

Any control (possibly except ActiveX, at present) that can be placed by Visual Studio's resource editor can also be used in a CRuntimeDlg based dialog.

It all adds up to being able to move the files anywhere without a second thought - works for me every time.

Implementation

This is a rather simplified diagram which shows the principal class relationships

{{DIAGRAM_START

: EasyFtp Class Diagram

                      .------------.
                      |CEasyFtpApp |
                      |            |
                      |Application |
                      |class       |
                      .v-----------·
                       |uses
                       |
                     .-v---------------.
                     |CRemoteFile      |
                     |                 |
                     |Orchestrates the |
                     |GUI and does the |
                     |uploading and    |
                  ---< downloading     >--------
                  |  ·-v---------------·       |
              uses|    |uses                   |uses
                  |    |                       |
   .--------------v    v------------------.    v--------------------.
   |CServerDialog |    |CRemoteFileDialog |    |CProgressDlg        |
   |              |    |                  |    |                    |
   |Retrieves the |    |Remote version of |    |Shows dload/uload   |
   |server details|    |CFileDialog       |    |progress and doubles|
   |              |    |                  |    | as a cancel dialog |
   ·-------------v·    ·v-----------------·    ·v-------------------·
                 |      |                       |
          derived|      |derived                |derived
             from|      |from                   |from
                 ------>v-----------------<------
                       |CRuntimeDlg       |
                       |                  |
                       |Implements dialogs|
                       | without resource |
                       |templates         |
                       |                  |
                       ·------------------·
}}DIAGRAM_END

(drawn courtesy of CodePlotter © AbstractSpoon 2003)

The rest of the code comprises of utility classes, the most interesting of which are:

  • CDeferWndMove

    Wrapper around ::DeferWindowPos() offering handy additions like OffsetCtrl() and ResizeCtrl().

  • CDlgUnits

    Wrapper around ::MapDialogRect() offering overloads to convert shorts, ints, longs, POINTs, SIZEs and RECTs to and from pixels and dialog units (DLUs).

  • CSysImageList

    Wrapper around the Windows system image list (which provides access to file icons).

  • CFileEdit

    CEdit derivative providing integrated browsing capabilities and using an enlarged non-client border in which to draw the file's icon.

Using EasyFtp (the utility)

By default, i.e. with nothing on the command line, EasyFtp will default to 'Download' mode, and will follow the workflow outlined above.

However, the following command line switches are available so that you can streamline or modify these defaults (note: switches must be preceded by - or /):

  • up

    Specifies you want to upload a file, with nothing else on the command line this will display the workflow in the article image.

  • rp

    Specifies the remote path to upload to, or download from. This can be a full path (less the server bit) or just a folder, in which case it needs a trailing forward slash.

  • lp

    Specifies the local path to upload from, or download to. This can be a full path or a folder (no trailing backslash required).

  • ag

    Specifies the agent string to use (useful if EasyFtp is being spawned by another app).

  • sv

    Specifies the server location e.g.. www.microsoft.com.

  • us

    Specifies the user name, if none is specified then an 'anonymous' login will be performed.

  • pw

    Specifies the password for the account, can be left blank if the username is blank.

  • an

    Specifies anonymous login, empty username/password strings are passed.

  • nl

    Specifies _not_ to convert filenames to lowercase when uploading.

  • nc

    Specifies suppression of the 'confirm overwrite' dialog which appears if the upload or download target path already exists.

Using the code

If you would rather integrate the code directly into your own application then it's equally easy.

  1. Take all the files from the shared folder (in the zip file) and copy them to a single location anywhere you want.
  2. To the file you want to have FTP support, add the line #include "[path]\remotefile.h"
  3. Finally, wherever you want to download or upload files, add the code like this:
    // note: this is straight out of EasyFtp.cpp
    
    // uploading a file (downloading is almost identical)
    // sLocalPath and sRemotePath will contain the user's 
    // choice on exit
    
    // or simply "rf.SetFile()"
    RMERR nErr = rf.SetFile(sLocalPath, sRemotePath); 
    
    // error handling
    switch (nErr)
    {
        case RMERR_SUCCESS:
            // note: if downloading we would now 
            // do something with the downloaded
            // file pointed to by sLocalPath
            break;
    
        case RMERR_USERCANCELLED:
            break;
    
        default:
        {
            CString sMessage;
    
            if (sLocalPath.IsEmpty())
               sMessage.Format("Sorry, the requested upload to '%s' could not /
                                be completed for the following reason:\n\n%s",
                                sServer, rf.GetLastError());
            else
               sMessage.Format("Sorry, the upload of '%s' to '%s' could not /
                                be completed for the following reason:\n\n%s",
                                sLocalPath, sServer, rf.GetLastError());
    
            AfxMessageBox(sMessage, MB_OK | MB_ICONEXCLAMATION);
        }
        break;
    }

History

  • 1.0 - 25th Feb, 2004
    • Initial release.
  • 1.0.1 - 26th Feb, 2004
    • fixed bug where server dialog still pops up after canceling file open dialog (uploads only).
    • fixed crash bug relating to not properly initializing the filename buffer pass to the file open dialog.
  • 1.0.2 - 26th Feb, 2004
    • fixed bug where Save As dialog was still appearing when the local path had been specified.
    • 'nl' switch added to prevent remote upload pathnames being converted to lowercase.
  • 1.1 - 10th Mar, 2004
    • support added for drag'n'drop onto the application icon (in explorer or on the desktop). Note: this means that once you have setup the app command line to point to your FTP server you can do drag and drop uploading from your desktop.
    • multiple files can now be uploaded or downloaded at once.
    • various UI related bugs fixed.
  • 1.3 - 23rd Mar, 2004 (not sure what happened to 1.2)
    • many bugs fixed.
    • anonymous login added. (Can be initialized using the -an command line switch.)
    • confirmation dialog added if target upload or download file(s) exist. (Can be suppressed using the -nc switch.)
    • improved error reporting, although I'm not yet translating HTTP errors.
  • 1.3.1 25th Mar, 2004
    • uxtheme.h/tmschema.h added to source code (thanks to Jay.ca).
  • 1.3.2 27th Mar, 2004
    • schemadef.h added to source code (thanks to Adnan).

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
Software Developer Maptek
Australia Australia
.dan.g. is a naturalised Australian and has been developing commercial windows software since 1998.

Comments and Discussions

 
Generalobtaining the file size Pin
mmontague19716-Dec-06 19:15
mmontague19716-Dec-06 19:15 
GeneralProblem with multiple files upload Pin
dejalab27-Feb-06 10:17
dejalab27-Feb-06 10:17 
GeneralMemory Leak Pin
gvf31-Dec-05 1:27
gvf31-Dec-05 1:27 
GeneralRe: Memory Leak Pin
gvf2-Jan-06 6:33
gvf2-Jan-06 6:33 
GeneralGood Demo Pin
petermxw6-Oct-05 23:50
petermxw6-Oct-05 23:50 
GeneralProgress Dialog - ETA Pin
DeanMarkTaylor17-Aug-05 3:59
DeanMarkTaylor17-Aug-05 3:59 
Generalinclude in c# Pin
sides_dale15-Jun-05 16:03
sides_dale15-Jun-05 16:03 
Generalnothing Pin
phyut24-May-05 22:44
phyut24-May-05 22:44 
Generalerror C2666 when building sample... Pin
Myron Belchmyer3-Dec-04 13:11
Myron Belchmyer3-Dec-04 13:11 
GeneralRe: error C2666 when building sample... Pin
.dan.g.5-Dec-04 23:35
professional.dan.g.5-Dec-04 23:35 
GeneralLooks great but I need to try it out Pin
sanjit_rath22-Nov-04 6:12
sanjit_rath22-Nov-04 6:12 
GeneralReal great tool... But Pin
diilbert22-Nov-04 5:58
diilbert22-Nov-04 5:58 
There is a problem. What if the FTP server is running on another port behind a firewall. It will need to be in passive mode for connections.

Is there any possiblity of this feature in the future.

Nice job, and by the way ToDoList keeps getting better and better Big Grin | :-D

-------
Removed... Too many questions Cool | :cool:
GeneralReal great tool... But Pin
diilbert22-Nov-04 5:58
diilbert22-Nov-04 5:58 
GeneralYes.... Pin
xblahx21-Jul-05 5:53
xblahx21-Jul-05 5:53 
GeneralSlick Pin
alex.barylski25-Oct-04 13:57
alex.barylski25-Oct-04 13:57 
Generalnc doesn't work Pin
dan_ds9-May-04 20:15
dan_ds9-May-04 20:15 
GeneralRe: nc doesn't work Pin
.dan.g.10-May-04 1:26
professional.dan.g.10-May-04 1:26 
GeneralTimeout Pin
dan_ds6-Apr-04 16:10
dan_ds6-Apr-04 16:10 
GeneralRe: Timeout Pin
.dan.g.6-Apr-04 20:56
professional.dan.g.6-Apr-04 20:56 
QuestionMissing Command Line Option? Pin
net4ward6-Apr-04 4:20
net4ward6-Apr-04 4:20 
AnswerRe: Missing Command Line Option? Pin
.dan.g.6-Apr-04 14:11
professional.dan.g.6-Apr-04 14:11 
GeneralThe missing file prob fixed but... Pin
Its Adnan28-Mar-04 20:11
Its Adnan28-Mar-04 20:11 
GeneralRe: The missing file prob fixed but... Pin
.dan.g.28-Mar-04 23:48
professional.dan.g.28-Mar-04 23:48 
Generalthanks?!! :) Pin
surgeproof26-Mar-04 6:20
surgeproof26-Mar-04 6:20 
GeneralRe: thanks?!! :) Pin
.dan.g.28-Mar-04 23:49
professional.dan.g.28-Mar-04 23:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.