Konuyu Oyla:
  • Derecelendirme: 0/5 - 0 oy
  • 1
  • 2
  • 3
  • 4
  • 5
Http aSync Download'ı Geliştirmek
#1
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
 
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



Kendime Göre Düzenlediğim Kod

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.


Ek Dosyalar
.zip   FDownloadDemo.zip (Dosya Boyutu: 1,79 KB / İndirme Sayısı: 13)
Cevapla


Konu ile Alakalı Benzer Konular
Konular Yazar Yorumlar Okunma Son Yorum
  TIdHTTPWebBrokerBridge ile HTTP server de response bloklanabilirmi? aegean 1 575 25-09-2025, Saat: 19:57
Son Yorum: aegean
  [Çözüldü] HTTP üzerinden RTSP ile IP kamera stream işlemek Abdullah ILGAZ 4 7.136 08-10-2024, Saat: 09:20
Son Yorum: sadikacar60
  HTTP/1.1 403 forbidden Yetkilendirme Hatası - [Çözüldü] Mesut 6 4.529 20-09-2022, Saat: 10:47
Son Yorum: Mesut
  Webbrowser üzerinden Download sorunu COMMANDX 3 2.286 05-05-2021, Saat: 09:57
Son Yorum: COMMANDX
  FDQuery async barisatalay 0 1.372 22-03-2020, Saat: 21:25
Son Yorum: barisatalay



Konuyu Okuyanlar: 1 Ziyaretçi