印地上传速度

发布于 2024-12-18 20:00:00 字数 384 浏览 2 评论 0原文

从这段代码中,我如何返回上传速度?

procedure TForm1.Button1Click(Sender: TObject);
begin

ftp.Host := 'domain';
ftp.Username := 'username';
ftp.password := 'password';
ftp.Connect;
ftp.Put('random-filename.ext'); //This is where it should grab only the latest file
//label1.caption := 'Download Speed: %s';  
ftp.Quit;
ftp.Disconnect;

end;

需要定时器吗?

谢谢。

From this code, how can I return the upload speed?

procedure TForm1.Button1Click(Sender: TObject);
begin

ftp.Host := 'domain';
ftp.Username := 'username';
ftp.password := 'password';
ftp.Connect;
ftp.Put('random-filename.ext'); //This is where it should grab only the latest file
//label1.caption := 'Download Speed: %s';  
ftp.Quit;
ftp.Disconnect;

end;

Would a timer be necessary?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别靠近我心 2024-12-25 20:00:00

假设您使用的是 TIdFTP 组件,则必须使用 OnWorkOnWorkBeginOnWorkEnd 事件来计算传输速度。

Assuming you are using a TIdFTP component then you must use the OnWork, OnWorkBegin and OnWorkEnd events to calculate the transfer rate.

望喜 2024-12-25 20:00:00

TIdFTP.OnWorkBegin 事件将告诉您将要发送多少字节,而 TIdFTP.OnWork 事件将告诉您在此期间到目前为止已发送了多少字节。转移。根据各个 OnWork 事件触发之间经过的时间以及每个事件之间传输的字节数的差异,您可以计算速度。

The TIdFTP.OnWorkBegin event will tell you how many bytes are going to be sent, and the TIdFTP.OnWork event will tell you how many bytes have been sent so far during the transfer. Based on how much time passes between individual OnWork event firings and the difference in transferred bytes between each one, you can calculate the speed.

摘星┃星的人 2024-12-25 20:00:00

不需要 TTimer 类,只需 Indy 组件 OnWorkBegin、OnWork 和 OnWorkEnd 事件。

您可以执行如下操作,但我建议您将 FTP 代码放入线程中,并使用 Synchronize 方法更新用户界面,以获得最佳性能。

interface 

uses
  // ...
  Windows;

type
  TForm1 = class(TForm)
  private
    startWriteTime : DWord;
    byteToWrite : Int64;

    procedure ftp_OnWorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64);
    procedure ftp_OnWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
    procedure ftp_OnWorkEnd(ASender: TObject; AWorkMode: TWorkMode);

    procedure displayWriteSpeed(byteWritten: Int64);
end;

implementation

uses
  // ...
  SysUtils;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ftp.OnWorkBegin := ftp_OnWorkBegin;
  ftp.OnWork := ftp_OnWork;
  ftp.OnWorkEnd := ftp_OnWorkEnd;
  // ...
  ftp.Connect;
  // ...
end;

procedure TForm1.ftp_OnWorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64);
begin
  if AWorkMode = wmWrite then
  begin
    byteToWrite := AWorkCountMax;
    startWriteTime := Windows.GetTickCount;
    displayWriteSpeed(-1);
  end;
end;

procedure TForm1.ftp_OnWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
begin
  if AWorkMode = wmWrite then
  begin
    displayWriteSpeed(AWorkCount);
  end;
end;

procedure TForm1.ftp_OnWorkEnd(ASender: TObject; AWorkMode: TWorkMode);
begin
  if AWorkMode = wmWrite then
  begin
    displayWriteSpeed(byteToWrite);
  end;
end;

procedure TForm1.displayWriteSpeed(byteWritten: Int64);
var
  elapsedMilliSeconds,  elapsedSeconds : Dword;
  speedBytesPerSeconds : Int64;
begin
  if byteWritten < 0 then
  begin
    {writeSpeedLabel.}Caption := 'upload speed: ?';
    Exit;
  end;

  elapsedMilliSeconds := Windows.GetTickCount - startWriteTime;
  elapsedSeconds := elapsedMilliSeconds div 1000;
  speedBytesPerSeconds := byteWritten div elapsedSeconds;
  {writeSpeedLabel.}Caption := SysUtils.Format('upload speed: %d b/s', [speedBytesPerSeconds ] );
end;

No TTimer class is necessary, only Indy component OnWorkBegin, OnWork and OnWorkEnd events.

You can do like below but I recommend you to put FTP code in a thread and to update user interface with the Synchronize method for best performance.

interface 

uses
  // ...
  Windows;

type
  TForm1 = class(TForm)
  private
    startWriteTime : DWord;
    byteToWrite : Int64;

    procedure ftp_OnWorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64);
    procedure ftp_OnWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
    procedure ftp_OnWorkEnd(ASender: TObject; AWorkMode: TWorkMode);

    procedure displayWriteSpeed(byteWritten: Int64);
end;

implementation

uses
  // ...
  SysUtils;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ftp.OnWorkBegin := ftp_OnWorkBegin;
  ftp.OnWork := ftp_OnWork;
  ftp.OnWorkEnd := ftp_OnWorkEnd;
  // ...
  ftp.Connect;
  // ...
end;

procedure TForm1.ftp_OnWorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64);
begin
  if AWorkMode = wmWrite then
  begin
    byteToWrite := AWorkCountMax;
    startWriteTime := Windows.GetTickCount;
    displayWriteSpeed(-1);
  end;
end;

procedure TForm1.ftp_OnWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
begin
  if AWorkMode = wmWrite then
  begin
    displayWriteSpeed(AWorkCount);
  end;
end;

procedure TForm1.ftp_OnWorkEnd(ASender: TObject; AWorkMode: TWorkMode);
begin
  if AWorkMode = wmWrite then
  begin
    displayWriteSpeed(byteToWrite);
  end;
end;

procedure TForm1.displayWriteSpeed(byteWritten: Int64);
var
  elapsedMilliSeconds,  elapsedSeconds : Dword;
  speedBytesPerSeconds : Int64;
begin
  if byteWritten < 0 then
  begin
    {writeSpeedLabel.}Caption := 'upload speed: ?';
    Exit;
  end;

  elapsedMilliSeconds := Windows.GetTickCount - startWriteTime;
  elapsedSeconds := elapsedMilliSeconds div 1000;
  speedBytesPerSeconds := byteWritten div elapsedSeconds;
  {writeSpeedLabel.}Caption := SysUtils.Format('upload speed: %d b/s', [speedBytesPerSeconds ] );
end;
你在看孤独的风景 2024-12-25 20:00:00

对于计时器部分:

您可以使用内置函数和全局变量:

var
  start, stop, elapsed: TDateTime;
  ielapsed: Integer;

// Start the timer
start := Now;
// Stop the timer
stop := Now;
elapsed := stop - start;
// Convert to milliseconds
ielapsed := round(elapsed * 60 * 60 * 24 * 1000);

由于 Windows API 函数 GetTickCount 已经以毫秒为单位,这是计算经过时间的简单方法,但仅限于 Windows 2000+:

var
  start, stop, elapsed: DWORD;

start := GetTickCount;
stop := GetTickCount;

if (stop > start) then
begin
  elapsed := stop - start;
end
else
begin
  // Handle overflow
  elapsed := MAXDWORD - start + stop;
end;

For the timer part:

You can use the built in functions and global variables:

var
  start, stop, elapsed: TDateTime;
  ielapsed: Integer;

// Start the timer
start := Now;
// Stop the timer
stop := Now;
elapsed := stop - start;
// Convert to milliseconds
ielapsed := round(elapsed * 60 * 60 * 24 * 1000);

Since the Windows API function GetTickCount is already in milliseconds, it's an easy way to calculate elapsed time, but only on Windows 2000+:

var
  start, stop, elapsed: DWORD;

start := GetTickCount;
stop := GetTickCount;

if (stop > start) then
begin
  elapsed := stop - start;
end
else
begin
  // Handle overflow
  elapsed := MAXDWORD - start + stop;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文