Firemonkey和TDownloadUrl [英] Firemonkey and TDownloadUrl

查看:89
本文介绍了Firemonkey和TDownloadUrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(Delphi XE2)VCL应用程序,其中包含对象TDownloadUrl(VCL.ExtActns)以检查多个网页,因此我想知道FireMonkey中是否存在等效的对象,因为我想利用此新功能的丰富功能平台。

I have an (Delphi XE2) VCL app containing an object TDownloadUrl (VCL.ExtActns) to check several webpages, so I wonder if there is an equivalent object in FireMonkey, 'cause I wanna take advantage of rich features from this new platform.

使用线程的Firemonkey应用程序演示将不胜感激。

A Firemonkey app demo using threads would appreciate. Thanks in advance.

推荐答案

FireMonkey尚不存在操作。

Actions don't exist yet with FireMonkey.

顺便说一句,您可以使用以下代码创建相同的行为:

BTW, you can create the same behavior with a code like this:

IdHTTP1: TIdHTTP;

...

procedure TForm2.MenuItem1Click(Sender: TObject);
const
  FILENAME = 'C:\Users\Whiler\Desktop\test.htm';
  URL      = 'http://stackoverflow.com/questions/7491389/firemonkey-and-tdownloadurl';
var
//  sSource: string;
  fsSource: TFileStream;
begin
  if FileExists(FILENAME) then
  begin
    fsSource := TFileStream.Create(FILENAME, fmOpenWrite);
  end
  else
  begin
    fsSource := TFileStream.Create(FILENAME, fmCreate);
  end;

  try
    IdHTTP1.Get(URL, fsSource);
  finally
    fsSource.Free;
  end;
//  sSource := IdHTTP1.Get(URL);
end;

如果只需要内存中的源代码,则注释行可以替换其他行。

The commented lines can replace the others if you just need the source in memory...

如果要使用线程,可以按以下方式进行管理:

If you want to use a thread, you can manage it like this:

unit Unit2;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, FMX.Menus;

type
  TDownloadThread = class(TThread)
  private
    idDownloader: TIdHTTP;
    FFileName   : string;
    FURL        : string;
  protected
    procedure Execute; override;
    procedure Finished;
  public
    constructor Create(const sURL: string; const sFileName: string);
    destructor  Destroy; override;
  end;
type
  TForm2 = class(TForm)
    MenuBar1: TMenuBar;
    MenuItem1: TMenuItem;
    procedure MenuItem1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.fmx}

procedure TForm2.MenuItem1Click(Sender: TObject);
const
  FILENAME = 'C:\Users\Whiler\Desktop\test.htm';
  URL      = 'http://stackoverflow.com/questions/7491389/firemonkey-and-tdownloadurl';
var
//  sSource: string;
  fsSource: TFileStream;
begin
  TDownloadThread.Create(URL, FILENAME).Start;
end;

{ TDownloadThread }

constructor TDownloadThread.Create(const sURL, sFileName: string);
begin
  inherited Create(true);
  idDownloader := TIdHTTP.Create(nil);
  FFileName       := sFileName;
  FURL            := sURL;
  FreeOnTerminate := True;
end;

destructor TDownloadThread.Destroy;
begin
  idDownloader.Free;
  inherited;
end;

procedure TDownloadThread.Execute;
var
//  sSource: string;
  fsSource: TFileStream;
begin
  inherited;
  if FileExists(FFileName) then
  begin
    fsSource := TFileStream.Create(FFileName, fmOpenWrite);
  end
  else
  begin
    fsSource := TFileStream.Create(FFileName, fmCreate);
  end;

  try
    idDownloader.Get(FURL, fsSource);
  finally
    fsSource.Free;
  end;
  Synchronize(Finished);
end;

procedure TDownloadThread.Finished;
begin
  // replace by whatever you need
  ShowMessage(FURL + ' has been downloaded!');
end;

end.

这篇关于Firemonkey和TDownloadUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆