om3raltiparmak 0 Posted March 5, 2022 (edited) Hi all, I tried to develop the HttpAsyncDownload sample application for myself in RAD Studio 10.4 Demos-master. The original application downloads the file from the URL contained in a single edit object. The filename is also being changed from another edit object. There is no error in the original application, it works smoothly. I wanted to encode the application for multi download and I added a listbox of random URLs in it. The application is running, but this time it thinks it has downloaded some files and gives the information that it is complete. It also downloads some files without any problems. Also, I added a code that will remove the edit object where the filename is written, to preserve the original filename in the URL. I am attaching the codes I edited below and the pas file of the original application. I will be glad if you can help me with the error. Example URL 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.pdf The Code I Edited By Myself 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. FDownloadDemo.zip Edited March 5, 2022 by om3raltiparmak Share this post Link to post