02-03-2022, Saat: 16:30
Merhaba,
RAD Studio 10.4 Demos-master içerisinde HttpAsyncDownload örnek uygulamasını kendime uygun geliştirmeye çalıştım.
Orjinal uygulama, bir tek edit nesnesi içerisinde yer alan URL'den dosyayı indiriyor. Dosya ismi de bir başka edit nesnesinden değiştiriliyor. Orjinal uygulamada haliyle hata yok. Sağlıklı çalışıyor.
Ben bunu tekli değil de çoklu download etmesini istedim ve içerisine rastgele URL'lerden bir listbox ekledim. Uygulama çalışıyor ancak bu kez bazı dosyaları indirdiğini zannederek tamamlandı bilgisini veriyor. Bazı dosyaları da gerçekten indiriyor. Ayrıca ben dosya adının yazıldığı edit nesnesini de kaldıracak URL'de ki orjinal dosya adını koruması için bir kod ekledim.
Aşağıya benim düzenlediğim kodları ve orjinal uygulamanın da pas dosyasını ekliyorum.
Hata konusunda yardımcı olursanız sevinirim.
Çağırdığım URL
Kendime Göre Düzenlediğim Kod
RAD Studio 10.4 Demos-master içerisinde HttpAsyncDownload örnek uygulamasını kendime uygun geliştirmeye çalıştım.
Orjinal uygulama, bir tek edit nesnesi içerisinde yer alan URL'den dosyayı indiriyor. Dosya ismi de bir başka edit nesnesinden değiştiriliyor. Orjinal uygulamada haliyle hata yok. Sağlıklı çalışıyor.
Ben bunu tekli değil de çoklu download etmesini istedim ve içerisine rastgele URL'lerden bir listbox ekledim. Uygulama çalışıyor ancak bu kez bazı dosyaları indirdiğini zannederek tamamlandı bilgisini veriyor. Bazı dosyaları da gerçekten indiriyor. Ayrıca ben dosya adının yazıldığı edit nesnesini de kaldıracak URL'de ki orjinal dosya adını koruması için bir kod ekledim.
Aşağıya benim düzenlediğim kodları ve orjinal uygulamanın da pas dosyasını ekliyorum.
Hata konusunda yardımcı olursanız sevinirim.
Çağırdığım URL
Kod: (Select All)
https://tdsoftware.files.wordpress.com/2010/10/delphi_giris.pdf
https://dtffvb2501i0o.cloudfront.net/images/rad-studio/rad-11/RAD_11_Visuals_Set_5.png
https://dtffvb2501i0o.cloudfront.net/images/rad-studio/rad-11/smartmockups_kt2ezvfb.png
https://d3rs4yn018y0eg.cloudfront.net/tutorial/images/1470835852.jpg
https://web.itu.edu.tr/~sakarya/files/delphi.pdfKendime Göre Düzenlediğim Kod
Kod: (Select All)
unit FDownloadDemo;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Net.URLClient, System.Net.HttpClient,
System.Net.HttpClientComponent, FMX.StdCtrls, FMX.Edit, FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo,
System.ImageList, FMX.ImgList, FMX.Memo.Types, FMX.Layouts, FMX.ListBox;
type
TFormDownloadDemo = class(TForm)
PanelTop: TPanel;
PanelCenter: TPanel;
LabelFile: TLabel;
BStartDownload: TButton;
Memo1: TMemo;
ImageList1: TImageList;
LabelURL: TLabel;
LabelGlobalSpeed: TLabel;
ProgressBarDownload: TProgressBar;
BStopDownload: TButton;
DPath: TEdit;
ListBox1: TListBox;
EditFileName: TEdit;
Label1: TLabel;
procedure BStartDownloadClick(Sender: TObject);
procedure ButtonCancelClick(Sender: TObject);
procedure BStartComponentClick(Sender: TObject);
procedure ReceiveDataEvent(const Sender: TObject; AContentLength: Int64; AReadCount: Int64; var Abort: Boolean);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
FClient: THTTPClient;
FGlobalStart: Cardinal;
FAsyncResult: IAsyncResult;
FDownloadStream: TStream;
procedure DoEndDownload(const AsyncResult: IAsyncResult);
public
{ Public declarations }
procedure SampleDownload;
end;
var
FormDownloadDemo: TFormDownloadDemo;
implementation
{$R *.fmx}
uses
System.IOUtils;
procedure TFormDownloadDemo.BStartDownloadClick(Sender: TObject);
begin
BStartDownload.Enabled := False;
SampleDownload;
end;
procedure TFormDownloadDemo.ButtonCancelClick(Sender: TObject);
begin
(Sender as TButton).Enabled := False;
FAsyncResult.Cancel;
end;
procedure TFormDownloadDemo.BStartComponentClick(Sender: TObject);
begin
BStartDownload.Enabled := False;
SampleDownload;
end;
procedure TFormDownloadDemo.DoEndDownload(const AsyncResult: IAsyncResult);
var
LAsyncResponse: IHTTPResponse;
begin
try
LAsyncResponse := THTTPClient.EndAsyncHTTP(AsyncResult);
TThread.Synchronize(nil,
procedure
begin
Memo1.Lines.Add('Download Finished!');
Memo1.Lines.Add(Format('Status: %d - %s', [LAsyncResponse.StatusCode, LAsyncResponse.StatusText]));
end);
finally
LAsyncResponse := nil;
FreeandNil(FDownloadStream);
BStopDownload.Enabled := False;
BStartDownload.Enabled := True;
end;
end;
procedure TFormDownloadDemo.FormCreate(Sender: TObject);
begin
FClient := THTTPClient.Create;
FClient.OnReceiveData := ReceiveDataEvent;
end;
procedure TFormDownloadDemo.FormDestroy(Sender: TObject);
begin
FDownloadStream.Free;
FClient.Free;
end;
procedure TFormDownloadDemo.ReceiveDataEvent(const Sender: TObject; AContentLength, AReadCount: Int64;
var Abort: Boolean);
var
LTime: Cardinal;
LSpeed: Integer;
begin
LTime := TThread.GetTickCount - FGlobalStart;
LSpeed := (AReadCount * 1000) div LTime;
TThread.Queue(nil,
procedure
begin
ProgressBarDownload.Value := AReadCount;
LabelGlobalSpeed.Text := Format('Global speed: %d KB/s', [LSpeed div 1024]);
end);
end;
procedure TFormDownloadDemo.SampleDownload;
var
URL: string;
LResponse: IHTTPResponse;
LFileName, Filename: string;
LSize: Int64;
i: integer;
begin
DPath.Text := TPath.GetDocumentsPath;
i := 0;
try
for i := 0 to ListBox1.Items.Count-1 do
begin
ListBox1.ItemIndex := i;
URL := ListBox1.Items[i];
Filename := StringReplace(URL, '/', '\', [rfReplaceAll]);
LFileName := TPath.Combine(DPath.Text, ExtractFileName(Filename));
EditFilename.Text := ExtractFileName(Filename);
LResponse := FClient.Head(URL);
LSize := LResponse.ContentLength;
Memo1.Lines.Add(Format('Head response: %d - %s', [LResponse.StatusCode, LResponse.StatusText]));
LResponse := nil;
ProgressBarDownload.Max := LSize;
ProgressBarDownload.Min := 0;
ProgressBarDownload.Value := 0;
LabelGlobalSpeed.Text := 'Global speed: 0 KB/s';
Memo1.Lines.Add(Format('Downloading: "%s" (%d Bytes) into "%s"' , [ExtractFileName(Filename), LSize, LFileName]));
// Create the file that is going to be dowloaded
FDownloadStream := TFileStream.Create(LFileName, fmCreate);
FDownloadStream.Position := 0;
FGlobalStart := TThread.GetTickCount;
// Start the download process
FAsyncResult := FClient.BeginGet(DoEndDownload, URL, FDownloadStream);
end;
finally
BStopDownload.Enabled := FAsyncResult <> nil;
BStartDownload.Enabled := FAsyncResult = nil;
end;
end;
end.
