Using ftplib, you can easily upload files and display the progress. The following is a piece of program code I wrote last weekend for your sharing:
Struct remfile {
Struct remfile * next;
Int fsz; // File Size
Char * FNM; // file name
};
// Send progress information to the main window for display
Static int log_progress (netbuf * CTL, int xfered, void * Arg)
{
Struct remfile * f = (struct remfile *) ARG;
Int PCT = (INT) (xfered * 1.0/F-> fsz* 100 );
Hwnd = (hwnd) (afxgetapp ()-> getmainwnd ()-> getsafehwnd ());
Postmessage (hwnd, wm_progress, PCT, null );
Return 1;
}
// FTP upload
Bool myftp: Up (const string & sourcefilename, const string & targetfilename)
{
Int STS = 0;
Int fsz;
If (! Connect ())
Return false;
Ftpoptions (ftplib_callback, (long) null, Conn );
Struct remfile * F;
F = (struct remfile *) malloc (sizeof (struct remfile ));
Memset (F, 0, sizeof (struct remfile ));
F-> FNM = strdup (sourcefilename. c_str ());
Fsz = tpub: getfilesize (F-> FNM );
F-> fsz = fsz;
Fsz = fsz/100;
If (fsz)
{
Ftpoptions (ftplib_callback, (long) log_progress, Conn );
Ftpoptions (ftplib_idletime, (long) 1000, Conn );
Ftpoptions (ftplib_callbackarg, (long) F, Conn );
Ftpoptions (ftplib_callbackbytes, (long) fsz, Conn );
}
STS = ftpput (F-> FNM, targetfilename. c_str (), 'I', Conn );
If (STS)
{
: Postmessage (hwnd) (afxgetapp ()-> getmainwnd ()-> getsafehwnd (), wm_progress, 100, null );
}
Free (F-> FNM );
Free (f );
If (! STS)
{
_ Err = "FTP error :";
_ Err = _ err + ftplastresponse (conn );
Return false;
}
Return true;
}
Where:
Hwnd = (hwnd) (afxgetapp ()-> getmainwnd ()-> getsafehwnd ());
Postmessage (hwnd, wm_progress, PCT, null );
Obtains the handle of the Main Window and sends a message to the main window. After receiving the message, the main window displays the running progress. The Code is as follows:
Lresult cftptestdlg: onprogress (wparam, lparam)
{
Updatedata (true );
Int progress = (INT) wparam;
String STR = "";
STR = STR + tpub: inttostring (Progress) + "% ";
M_percent = Str. c_str ();
Updatedata (false );
Return 0;
}
After running the program, the effect is as follows:
Please criticize the incorrect content.