Placing Files on an FTP Server
A version of this page is also available for
4/8/2010
There are two methods for placing a file on an FTP server using the Windows Internet Services (WinInet) functions:
- Use FtpOpenFile with InternetWriteFile.
- Use FtpPutFile.
An application that must send data to an FTP server, but does not have a local file containing all the data, should use FtpOpenFile to create and open a file on the FTP server. The application then can use InternetWriteFile to upload the information to the file.
If the file already exists locally, the application can use FtpPutFile to upload the file to the FTP server. FtpPutFile performs all actions associated with uploading a local file to a remote FTP server.
The following example shows how to place the file indicated by the IDC_FTPEdit2 edit box on the FTP server, using the file name specified by the IDC_FTPEdit3 edit box. The HINTERNET handle hSecondary was created by InternetConnect after establishing an FTP session.
int WINAPI PutFile(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","Put File",MB_OK);
return 0;
}
else
{
intTransType = MessageBox(hX,
"Do you want to upload in ASCII (Default:Binary)?",
"Put File",MB_YESNO);
if (intTransType==IDYES)
{
if(!FtpPutFile(hSecondary,strInFile,strOutFile,
FTP_TRANSFER_TYPE_ASCII,0))
{
ErrorOut(hX,GetLastError(),"Get File");
return 0;
}
else
{
MessageBox(hX,"ASCII Transfer Complete","Put File",MB_OK);
return 1;
}
}
else
{
if(!FtpPutFile(hSecondary,strInFile,strOutFile,
FTP_TRANSFER_TYPE_BINARY,0))
{
ErrorOut(hX,GetLastError(),"Get File");
return 0;
}
else
{
MessageBox(hX,"Binary Transfer Complete","Get File",MB_OK);
DisplayDir(hX,INTERNET_FLAG_RELOAD);
return 1;
}
}
}
}//end