Receiving files
function Tform1.geturlfilename (aurl:string): string;
Var
I:integer;
s:string;
Begin//Return file name
s: = Aurl;
I: = Pos ('/', s);
While I <> 0 do//Remove the contents of the front of the "/" the rest is the file name.
Begin
Delete (S, 1, i);
I: = Pos ('/', s);
End
Result: = s;
End
Get File size
function Tform1.getfilesize (aurl:string): integer;
Var
Filesize:integer;
Tstream:tfilestream;
filename:string;
Begin
Tstream.size: = 0;
Idftp1.structuremount (Aurl); ******************************** don't know what it's for, right?
FileSize: = IdFTP1.Response.ContentLength;
FileSize: = Idftp1.size (FileName);
FileSize: = Idftp1.contentlength (FileName);
Idftp1.abort;
Result: = FileSize;
End
Multi-Threaded Download
Procedure Tform1.button11click (Sender:tobject);
Var
M:integer;
Begin
ShowMessage (' ok! main thread is executing, get filename and display in Edit5 ');
Aurl: = Edit4.text; FTP mode
Afile: = Geturlfilename (Edit4.text);//Get file name
xx:= Strtoint (Edit5.text); Number of threads entered
M:=1;
Afilesize: = GetFileSize (Aurl);
Avg: = Trunc (AFILESIZE/XX);
Try
GetThread ();
While M<=xx do
Begin
MYTHREAD[M]. Resume; Wake-Up thread
M: =m+1;
End
Except
ShowMessage (' Create thread failed! ');
Exit;
End
End
Before you begin the download, set the maximum value of ProgressBar1 to the size of the data that you want to receive. *******************
Procedure Tform1.idftp1workbegin (Sender:tobject; Aworkmode:tworkmode;
Const Aworkcountmax:integer);
Begin
Aborttransfer: = False;
Progressbar1.max:=aworkcountmax;
progressbar1.min:=0;
progressbar1.position:=0;
End
Status display
Procedure Tform1.idftp1status (Asender:tobject;const astatus:tidstatus;
Const astatustext:string);
Begin
Listbox1.itemindex: =listbox1.items.add (Astatustext);
End
The generation of multithreading
Procedure Tform1.getthread ();
Var
I, Start,last:integer;
filename:string;
Begin
I:=1;
While I<=xx do
Begin
If I=1 Then
Begin
Start: = 0;
Last: = Avg*i;
End
Else
Start: = avg* (i-1);
Last: = Avg*i;
Filename:=afile+inttostr (i);
Mythread[i]:=tthread1.create (Aurl,afile,filename, False, I,start,last);
I: =i+1;
End
End
constructor function
Constructor Tthread1.create (aurl,afile,filename:string; bresume:boolean; Count,start,last:integer);
Begin
Inherited create (true);
Freeonterminate: = true;
Turl: = Aurl;
Tfile: = Afile;
TCount: = Count;
Tresume: = Bresume;
Tstart: =start;
Tlast: =last;
Temfilename:= FileName;
End
Download file functions
Procedure Tthread1.downlodefile ();
Var
Ftp:tidftp;
Tidftp1:tidftp;
Tstream:tfilestream;
Begin
TIdFTP1: = Tidftp.create (nil);
form1.idantifreeze1.onlywhenidle:=false;//setting causes the program to react
If FileExists (temfilename) then//if the file already exists
TStream: = Tfilestream.create (temfilename,fmopenwrite) Else
TStream: = Tfilestream.create (temfilename,fmcreate);
If Tresume then//continuation mode
Begin
Exit
End else//overwrite or new mode
Begin
Tidftp1.maxlinelength: = Tstart; (no) where does the ******************** file download start with the Tidftp attribute set?
Tidftp1.minlinelength: = Tlast; (no) where is the end location of the ********************* file download set with the Tidftp attribute?
End
Try
Tidftp1.get (temfilename,tstream,true); Start download
Tidftp1.get (Turl,tstream); Start download
Form1.ListBox1.ItemIndex: =form1.listbox1.items.add (temfilename+ ' Download ');
Finally
Tstream.free;
End
End
Procedure Tthread1.execute;
Begin
If Form1.edit4.text<> ' Then
Synchronize (Downlodefile)
Else
Exit
End
Using Delphi to implement FTP multi-threaded download source code (GO)