S
sforever
Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手,我下面这段代码呢,据说是高手唐晓锋的代码,是关于网络文件传输的。
请各位能不能帮助我加上文件的传输进度啊。。。在服务器端加上文件的传输进度显示。。。
我水平太菜,实验了很多方法。都不能成功。。还有一点。能不能修改成弹出保存文件的对话框来保存文件。。这个上面默认是保存在C盘根目录下的,谁能给修改一点。。多谢了。
客户端(发送文件的)
unit mail;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, ComCtrls;
type
TCon = record
FileName : String;
TotalSize : Integer;
Status : Integer;
end;
PCON = ^TCON;
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Edit3: TEdit;
OpenDialog1: TOpenDialog;
StatusBar1: TStatusBar;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var Count : Integer;
function GetFileSize(const FileName: string):integer;
var f : TFileStream;
begin
f := TFileStream.Create(FileName,fmOpenRead or fmShareDenyNone);
Result :=f.Size;
F.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if clientsocket1.Socket.Connected=false then
begin
ClientSocket1.Address:=edit1.Text;
ClientSocket1.Port :=strtoint(edit2.Text);
ClientSocket1.Open;
end;
with OpenDialog1 do
begin
Execute;
if FileName <> '' then
begin
Edit3.Text :='UPLOAD '+ ExtractFileName(FileName) +' '+Inttostr(GetFileSize(FileName));
Label3.Caption := FileName;
ClientSocket1.Socket.SendText(edit3.Text);
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var stream : TFileStream;
Buf : pointer;
begin
stream := TFileStream.Create(label3.Caption ,fmOpenRead or fmShareDenyNone);
GetMem(Buf,stream.Size);//建立一指定大小的动态变量,并将指针指向该处
stream.Seek(0,soFromBeginning);//移动流中指针的位置,移动后指针距离数据开始的位置
stream.ReadBuffer(Buf^,stream.Size);//从流中当前位置读取流的大小
ClientSocket1.Socket.SendBuf(Buf^,stream.Size);//发送流的大小
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ErrorCode := 0;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
StatusBar1.Align:=alBottom;
StatusBar1.SimplePanel := True;
end;
end.
服务器端(接收文件的)
unit mail;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, ComCtrls;
type
TCon = record
FileName : String;
TotalSize : Integer;
Status : Integer;
end;
PCON = ^TCON;
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
procedure FormCreate(Sender: TObject);
procedure ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var Count : Integer;
procedure TForm1.FormCreate(Sender: TObject);
begin
Count := 0;
if serversocket1.Active=false then
begin
serversocket1.Port := 7890; //端口
ServerSocket1.Open; //Socket开始侦听
end;
end;
procedure TForm1.ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ErrorCode := 0;
end;
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var c : pcon;
begin
c :=new(pcon);
c.FileName := '';
c.TotalSize := 0 ;
c.Status := 0;
Socket.Data := c;
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var C : PCON;
cmd:String;
Buffer : pointer;
n : integer;
fs : TFileStream;
const bufferSize = 1024 ;
begin
C:= Socket.Data ;
case c.Status of
0 :
begin
cmd := trim(Socket.ReceiveText) ;
if Pos('UPLOAD ',uppercase(cmd)) > 0 then
begin
c.FileName := trim(Copy(cmd,Pos(' ',cmd)+1,Length(cmd)));
c.TotalSize := StrToInt(Copy(c.FileName,Pos(' ',c.FileName)+1,Length(c.FileName)));
c.FileName := trim(Copy(c.FileName,1,Pos(' ',c.FileName)));
c.Status := 1;
Socket.Data := C;
end;
end;
1 :
begin
Count := count + 1; //发送的次数
GetMem(Buffer,BufferSize); //建立一指定大小的动态变量,并将指针指向该处
n := Socket.ReceiveBuf(Buffer^,BufferSize);//每次发送字节的大小 //接收数据包并读入缓冲区内
if not FIleExists('c:/'+c.FileName) then
begin
fs :=TFileStream.Create('c:/'+c.FileName,fmCreate or fmShareDenyNone);
fs.Seek(0,soFromBeginning);
end
else
begin
fs :=TFileStream.Create('c:/'+c.FileName,fmOpenWrite or fmShareDenyNone);
fs.Seek(0,soFromEnd);
end;
fs.WriteBuffer(Buffer^,n);
fs.Destroy;
FreeMem(Buffer);
end;
end;
end;
end.
请各位能不能帮助我加上文件的传输进度啊。。。在服务器端加上文件的传输进度显示。。。
我水平太菜,实验了很多方法。都不能成功。。还有一点。能不能修改成弹出保存文件的对话框来保存文件。。这个上面默认是保存在C盘根目录下的,谁能给修改一点。。多谢了。
客户端(发送文件的)
unit mail;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, ComCtrls;
type
TCon = record
FileName : String;
TotalSize : Integer;
Status : Integer;
end;
PCON = ^TCON;
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Edit3: TEdit;
OpenDialog1: TOpenDialog;
StatusBar1: TStatusBar;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var Count : Integer;
function GetFileSize(const FileName: string):integer;
var f : TFileStream;
begin
f := TFileStream.Create(FileName,fmOpenRead or fmShareDenyNone);
Result :=f.Size;
F.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if clientsocket1.Socket.Connected=false then
begin
ClientSocket1.Address:=edit1.Text;
ClientSocket1.Port :=strtoint(edit2.Text);
ClientSocket1.Open;
end;
with OpenDialog1 do
begin
Execute;
if FileName <> '' then
begin
Edit3.Text :='UPLOAD '+ ExtractFileName(FileName) +' '+Inttostr(GetFileSize(FileName));
Label3.Caption := FileName;
ClientSocket1.Socket.SendText(edit3.Text);
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var stream : TFileStream;
Buf : pointer;
begin
stream := TFileStream.Create(label3.Caption ,fmOpenRead or fmShareDenyNone);
GetMem(Buf,stream.Size);//建立一指定大小的动态变量,并将指针指向该处
stream.Seek(0,soFromBeginning);//移动流中指针的位置,移动后指针距离数据开始的位置
stream.ReadBuffer(Buf^,stream.Size);//从流中当前位置读取流的大小
ClientSocket1.Socket.SendBuf(Buf^,stream.Size);//发送流的大小
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ErrorCode := 0;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
StatusBar1.Align:=alBottom;
StatusBar1.SimplePanel := True;
end;
end.
服务器端(接收文件的)
unit mail;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, ComCtrls;
type
TCon = record
FileName : String;
TotalSize : Integer;
Status : Integer;
end;
PCON = ^TCON;
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
procedure FormCreate(Sender: TObject);
procedure ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var Count : Integer;
procedure TForm1.FormCreate(Sender: TObject);
begin
Count := 0;
if serversocket1.Active=false then
begin
serversocket1.Port := 7890; //端口
ServerSocket1.Open; //Socket开始侦听
end;
end;
procedure TForm1.ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ErrorCode := 0;
end;
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var c : pcon;
begin
c :=new(pcon);
c.FileName := '';
c.TotalSize := 0 ;
c.Status := 0;
Socket.Data := c;
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var C : PCON;
cmd:String;
Buffer : pointer;
n : integer;
fs : TFileStream;
const bufferSize = 1024 ;
begin
C:= Socket.Data ;
case c.Status of
0 :
begin
cmd := trim(Socket.ReceiveText) ;
if Pos('UPLOAD ',uppercase(cmd)) > 0 then
begin
c.FileName := trim(Copy(cmd,Pos(' ',cmd)+1,Length(cmd)));
c.TotalSize := StrToInt(Copy(c.FileName,Pos(' ',c.FileName)+1,Length(c.FileName)));
c.FileName := trim(Copy(c.FileName,1,Pos(' ',c.FileName)));
c.Status := 1;
Socket.Data := C;
end;
end;
1 :
begin
Count := count + 1; //发送的次数
GetMem(Buffer,BufferSize); //建立一指定大小的动态变量,并将指针指向该处
n := Socket.ReceiveBuf(Buffer^,BufferSize);//每次发送字节的大小 //接收数据包并读入缓冲区内
if not FIleExists('c:/'+c.FileName) then
begin
fs :=TFileStream.Create('c:/'+c.FileName,fmCreate or fmShareDenyNone);
fs.Seek(0,soFromBeginning);
end
else
begin
fs :=TFileStream.Create('c:/'+c.FileName,fmOpenWrite or fmShareDenyNone);
fs.Seek(0,soFromEnd);
end;
fs.WriteBuffer(Buffer^,n);
fs.Destroy;
FreeMem(Buffer);
end;
end;
end;
end.