Compartir a través de


Retrieving Files from an FTP Server

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

The Windows Internet Services (WinInet) functions offer three methods for retrieving files from an FTP server:

If the URL of the file is available, the application can call InternetOpenUrl to connect to that URL and then use InternetReadFile to control the download of the file. This setup allows the application tighter control over the download and is ideal for situations where no other operations need to be made on the FTP server. For more information about how to directly access resources, see Accessing URLs Directly.

If the application has established an FTP session handle to the server with InternetConnect, the application can call FtpOpenFile with the existing file name and with a new name for the locally stored file. The application can then use InternetReadFile to download the file. This allows the application tighter control over the download and keeps the connection to the FTP server, so more commands can be executed.

If the application does not need tight control over the download, the application can use FtpGetFile with the FTP session handle, remote file name, and local file name to retrieve the file. FtpGetFile performs all the bookkeeping and overhead associated with reading a file from an FTP server and storing it locally.

The following example shows how to retrieve the file indicated by the IDC_FTPEdit2 edit box and stores it locally using the file name specified by the IDC_FTPEdit3 edit box. The HINTERNET handle hSecondary was created by InternetConnect after establishing an FTP session. DisplayDir is another function that is designed to enumerate the directory.

int WINAPI GetFile(HWND hX)
{
    char strInFile[80];
    char strOutFile[80];
    int intTransType;
    strInFile[0]=0;
    strOutFile[0]=0;

    GetDlgItemText(hX,IDC_FTPEdit3,strOutFile,80);
    GetDlgItemText(hX,IDC_FTPEdit2,strInFile,80);

    if ((strlen(strOutFile)==0) || (strlen(strInFile)==0))
    {
        MessageBox(hX,"Target File or Destination File Missing","Get File",MB_OK);
        return 0;
    }
    else
    {
        intTransType = MessageBox(hX,
            "Do you want to download in ASCII (Default:Binary)?",
            "Get File",MB_YESNO);
        if (intTransType==IDYES)
        {
            if(!FtpGetFile(hSecondary,strInFile,strOutFile,FALSE,
                FILE_ATTRIBUTE_NORMAL,FTP_TRANSFER_TYPE_ASCII | 
                INTERNET_FLAG_NO_CACHE_WRITE,0))
            {
                ErrorOut(hX,GetLastError(),"Get File");
                DisplayDir(hX,INTERNET_FLAG_RELOAD);
                return 0;
            }
            else
            {
                MessageBox(hX,"ASCII Transfer Complete","Get File",MB_OK);
                return 1;
            }
        }
        else
        {
            if(!FtpGetFile(hSecondary,strInFile,strOutFile,FALSE,
                FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY |
                INTERNET_FLAG_RELOAD,0))
            {
                ErrorOut(hX,GetLastError(),"Get File");
                return 0;
            }
            else
            {
                MessageBox(hX,"Binary Transfer Complete","Get File",MB_OK);
                return 1;
            }
        }
    }
}

See Also

Concepts

FTP Sessions