IdTCPClient传输文件出错

lupingyang 2010-04-30 12:25:49
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, ComCtrls;

type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
IdTCPClient1: TIdTCPClient;
Button1: TButton;
edtFileName: TEdit;
edtAddress: TEdit;
Button2: TButton;
ProgressBar1: TProgressBar;
StatusBar1: TStatusBar;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
OpenDialog1.Execute;
edtFileName.Text:=OpenDialog1.FileName
end;

procedure TForm1.Button2Click(Sender: TObject);
var
iFileHandle:integer;
iFileLen,cnt:integer;
buf:array[0..4096] of byte;
begin
if (edtAddress.Text<>'')and (edtFileName.Text<>'') then
begin
IdTCPClient1.Host:=edtAddress.Text;
IdTCPClient1.Port:=9925;
try
IdTCPClient1.Connect(5000);
except
StatusBar1.Panels[0].Text:='连接接受方失败!';
exit;
end;
if IdTCPClient1.Connected then
begin
iFileHandle:=FileOpen(edtFileName.Text,fmOpenRead);
iFileLen:=FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
ProgressBar1.Max:=iFileLen;
ProgressBar1.Position := 0;
IdTCPClient1.WriteLn(ExtractFileName(edtFileName.Text)+'|'+IntToStr(iFileLen));
while true do
begin
Application.ProcessMessages;
cnt:=FileRead(iFileHandle,buf,4096);
IdTCPClient1.WriteBuffer(buf,cnt);
IdTCPClient1.WriteInteger(cnt);
ProgressBar1.Position:=ProgressBar1.Position + cnt;
StatusBar1.Panels[0].Text:='正在传送文件...';
if cnt<4096 then
break;
end;
FileClose(iFileHandle);
Label2.Caption:='文件传送完成!';
StatusBar1.Panels[0].Text:='文件传送完成!';
end;
end
else
ShowMessage('请选择要传送的文件和或接受方地址');

end;

end.



错误:list index out of bounds(0)
...全文
95 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
李_军 2010-04-30
  • 打赏
  • 举报
回复
单步调试下,检查是不是StatusBar1.Panels这个控件你根本没增加Panel

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧