Ftplib practice-Upload

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.