Delphide bir download classı yazdım kendim için.(Acemiyim eksik yanları mutlaka vardır. Hatta bir dünya vardır diyeyim ) sizlerle de paylaşmak istedim.
Sınıf ne işe yarar:
- Bir Url deki dosyayı indirmenize yarar. Aynı zamanda parametre olarak vereceğiniz bir progressbarı'da kontrol eder indirmeye göre.
Sınıf;
Kod: Tümünü seç
unit Download;
interface
Uses
System.SysUtils, System.Classes, Vcl.Forms, Vcl.Dialogs, IdComponent, IdHTTP, Vcl.ComCtrls;
type
FileDownload = class
public
function DownloadFile(Url: String; SFile: String; &ProgressBar: TProgressBar): Boolean;
procedure HttpWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
Destructor destroy;
private
Bar : TProgressBar;
Http : TIdHTTP;
DFile: TFileStream;
End;
implementation
function FileDownload.DownloadFile(Url:string; SFile: String; &ProgressBar: TProgressBar): Boolean;
begin
try
Bar := ProgressBar;
DFile := TFileStream.Create(Sfile,fmCreate);
Http := TIdHTTP.Create();
Http.OnWork := HttpWork;
Http.Get(Url, DFile);
Result := True;
Except On E: Exception Do
ShowMessage(E.Message);
end;
end;
procedure FileDownload.HttpWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
begin
try
Bar.Position := round(( aworkcount * 100 ) / DFile.Size );
Bar.Update;
Application.ProcessMessages;
Except On E: Exception Do
ShowMessage(E.Message);
end;
end;
Destructor FileDownload.destroy;
begin
Dfile.Free;
Http.Free;
end;
end.
Kullanımı; Bir Butonun içine de yazabilirsiniz. Global olarakta tanımlayabilirsiniz;
Kod: Tümünü seç
var
Fdown : FileDownload;
begin
Fdown := FileDownload.create;
if Fdown.DownloadFile('http://www.site.com/deneme.rar','c:\deneme.rar',Progressbar1) = True then
ShowMessage('Dosya İndirildi');
Fdown.Free;
end;