Delphi implementation for FTP and HTTP resumable upload and download

Source: Internet
Author: User
FTP and HTTP breakpoint resume and download Delphi achieve reprint: http://blog.csdn.net/youthon/article/details/7531211 classification: Delphi programming network programming people read comments (1) collection reports delphiintegerstring server resturl

Next, let's write the most important code, that is, the download part. First, let's look at the HTTP protocol:

Procedure tform1.httpdownload (aurl, afile: string; bresume: Boolean );
VaR tstream: tfilestream;
Begin // HTTP download
If fileexists (afile) Then // if the file already exists
Tstream: = tfilestream. Create (afile, fmopenwrite)
Else
Tstream: = tfilestream. Create (afile, fmcreate );
If bresume then // resumable Data Transfer Method
Begin
Idhttp1.request. contentrangestart: = tstream. Size-1;
Tstream. Position: = tstream. Size-1; // move to the end to continue downloading
Idhttp1.head (aurl );
Idhttp1.request. contentrangeend: = idhttp1.response. contentlength;
End
Else // overwrite or create Mode
Begin
Idhttp1.request. contentrangestart: = 0;
End;
Try
Idhttp1.get (aurl, tstream); // start download
Finally
Tstream. Free;
End;
End;
Here we also use the idhttp get process. The aurl of the function is the URL, the afile is the saved file name, And bresume determines whether to resume data transfer. Note that the code in the resume mode is: idhttp1.request. contentrangestart: = tstream. size-1; tstream. position: = tstream. size-1; // move to the end to continue downloading idhttp1.head (aurl); idhttp1.request. contentrangeend: = idhttp1.response. contentlength; In the first line, we set the start position of the download to the end of the file stream to the size of the part of the file that has been downloaded, the second line points the file stream to its end, and the third line gets the URL header information through the head process, in the fourth row, the total size of the header information is assigned to the end position of the download. Here, why do the first and second lines of code end- 1. When I didn't add-1, an error is always prompted when I continued to download a complete downloaded file, finally, the Code tracking idhttp finds that when processing the download range, if the start position is the same as the end position, it will cause an error of converting the floating point number to an integer, therefore, we can add-1 here to prevent this error. Another method is to compare it to exit if the start position is equal to the end position. Let's take a look at the FTP download process: Procedure tform1.ftpdownload (aurl, afile: string; bresume: Boolean); var tstream: tfilestream; sname, spass, shost, sport, sdir: string; begin // FTP download if fileexists (afile) Then // create a file stream tstream: = tfilestream. create (afile, fmopenwrite) else tstream: = tfilestream. create (afile, fmcreate );
Getftpparams (aurl, sname, spass, shost, sport, sdir); with idftp1 do try if connected then disconnect; // reconnect Username: = sname; Password: = spass; Host: = shost; port: = strtoint (sport); Connect; doesn t exit; end;
Idftp1.changedir (sdir); // change the directory bytestotransfer: = idftp1.size (afile); try if bresume then // resume begin tstream. position: = tstream. size; idftp1.get (afile, tstream, true); End else begin idftp1.get (afile, tstream, false); end; finally tstream. free; end; in this process, the getftpparams () function is used to separate the URL user name, password, host address, port, path, and other information, idftp uses this information to log on to the server and go to the corresponding directory. Finally, the get () process is easy to download. Its resume is much easier than HTTP, because the idftp get () itself supports continuous transmission. Here I will simply insert a bit of content. If a server supports resumable data transfer, we can send the "Rest 1" FTP command to check whether it supports resumable data transfer. If 350 is returned, it indicates it supports data transfer. Finally, we determine the protocol used for download Based on the website: function tform1.getprot (aurl: string): byte; begin // checks whether the download address is HTTP or FTP result: = 0; if pos ('HTTP ', lowercase (aurl) = 1 then result: = 1; // HTTP protocol if pos ('ftp', lowercase (aurl )) = 1 then result: = 2; // FTP protocol end; this function returns an integer Based on the URL for our use. Procedure tform1.mydownload (aurl, afile: string; bresume: Boolean); begin case getprot (aurl) of 0: showmessage ('unrecognized address! '); 1: httpdownload (aurl, afile, bresume); 2: ftpdownload (aurl, afile, bresume); end; this process uses getprot () the integer returned by the function is used to download the corresponding protocol. (2) Next let's look at the code of each button. With the above function, the code of the button is much simpler: Download button: Procedure tform1.button1click (Sender: tobject); var aurl, afile: string; begin aurl: = combobox1.text; //, for example, "http://www.2ccc.com/update/demo.exe"; afile: = geturlfilename (aurl); // get the file name, for example, "demo.exe" If fileexists (afile) then begin case messagedlg ('the file already exists. Do you want to resume? ', Mtconfirmation, mbyesnocancel, 0) of mryes: mydownload (aurl, afile, true); // resume mrno: mydownload (aurl, afile, false); // overwrite mrcancel: exit; // cancel end; end else mydownload (aurl, afile, false); // create a new object to download end; messagedlg () in the function pop-up dialog box, you can choose resume, overwrite, or cancel download. Interrupt button: Procedure tform1.button2click (Sender: tobject); begin aborttransfer: = true; end; I forgot to introduce it. So we cannot understand it here. aborttransfer is a private variable we have defined, set it to false when the download starts. This variable is monitored at any time during the download process. Once it becomes true, the download is interrupted using the idhttp disconnect and idftp1 abort methods, if the download is interrupted, an incomplete program or other things will be downloaded in the program directory, next time we download the package, we can choose to resume the download process. Procedure tform1.idhttp1workbegin (Sender: tobject; aworkmode: tworkmode; const aworkcountmax: integer); begin aborttransfer: = false ;...... End; In the onworkbegin events of idhttp1 and idftp, we set aborttransfer to false. In their work events, we detect the aborttransfer variable to complete the interrupted operation. Procedure destroy (Sender: tobject; aworkmode: tworkmode; const aworkcount: integer); begin if aborttransfer then begin // interrupt download idhttp1.disconnect; abort; end; progressbar1.position: = aworkcount; application. processmessages; end; (3) code for connection status and other information: Write procedure tform1.idhttp1status (asender: tobject; const AStatus: tidstatus; const astatustext: string); beg In listbox1.itemindex: = listbox1.items. add (astatustext); end; Because idhttp and idftp run the same code on onwork, onstatus, and other events, we only need to write one of them, then you can select the same event. Figure 8.3.4 3. After all the code is completed, run F9 to check whether resumable data transfer can be performed. [Program Summary] The main functions of this program are completed by the idhttp and idftp components. They mainly master the methods for resumable data transfer and string analysis and decomposition in their get process, here we also use the stream format, but this time it is not a memory stream but a file stream. In this example, you should have a preliminary understanding of the use of breakpoints and sharing of Event code during program debugging. [Post-author] shortly after writing this article, the author accidentally checked the help of the Indy series components and found a tiduri class that encapsulates the URL structure. In the iduri unit, this class can easily implement the functions of the above getftpparams () function, such as var uri: tiduri; begin URI: = tiduri. create (aurl); // create try sprotocol: = Uri. protocol; // protocol shost: = Uri. host; // host //...... And so on, you can get finally URI. Free; end through the URI attribute. Using this class of program can make it easier. How to modify it should be left to the readers themselves.

Delphi implementation for FTP and HTTP resumable upload and download

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.