各位老大,菜的无比的问题,传送文件的问题,我实在不知道该怎么写才对,请各位大哥指正一下,我知道是错的。
发送
procedure TForm1.Button1Click(Sender: TObject);
var
c:tmemorystream;
begin
c:=tmemorystream.Create;
if opendialog1.Execute then
c.SaveToFile(opendialog1.FileName);
serversocket1.Socket.Connections[0].SendStream(c);
end;
接收
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
a:array[1..10000] of byte;
i:integer;
q:tmemorystream;
begin
q:=tmemorystream.Create;
i:=socket.ReceiveLength;
socket.ReceiveBuf(a,i);
q.Write(a,i);
if q.Size>=i then
//这后面我就完全不知道如何写了,请各位大哥贴一段简单的文件传送,非常感谢。
begin
if savedialog1.Execute then
savedialog1.Files.SaveToStream(q);
end;
end;
发送
procedure TForm1.Button1Click(Sender: TObject);
var
c:tmemorystream;
begin
c:=tmemorystream.Create;
if opendialog1.Execute then
c.SaveToFile(opendialog1.FileName);
serversocket1.Socket.Connections[0].SendStream(c);
end;
接收
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
a:array[1..10000] of byte;
i:integer;
q:tmemorystream;
begin
q:=tmemorystream.Create;
i:=socket.ReceiveLength;
socket.ReceiveBuf(a,i);
q.Write(a,i);
if q.Size>=i then
//这后面我就完全不知道如何写了,请各位大哥贴一段简单的文件传送,非常感谢。
begin
if savedialog1.Execute then
savedialog1.Files.SaveToStream(q);
end;
end;
http://www.nssoft.net/showdoc.asp?did=656
http://www.nssoft.net/showdoc.asp?did=860
var
f:Tfilestream;
size:integer;
buf:olevariant;
p:pointer;
v:array of byte;
filen,fullpath:string;
begin
filen:=ClientFilename;
fullpath:= ExtractFileDir(ClientFilename);
if not DirectoryExists(fullpath) then
if not ForceDirectories(fullpath) then
begin
result:=error_mkdir;
exit;
end;
begin
size:= 0;
size := MainFrm.Monitor1.getfilebuf(buf,ServerFileName);
result:= size; //返回值《0 有问题
if size >= 0 then
begin
if fileexists(fullpath) then
f:=Tfilestream.Create(filen,fmOpenWrite)
else
f:=Tfilestream.Create(filen,fmCreate);
try
p:=vararraylock(buf);
setlength(v,size);
move(p^,v[0],size);
vararrayunlock(buf);
f.Write(v[0],size);
finally
f.Free;
end;
end;
end;
result := size;
end;
const filename: WideString): Integer;
var f:Tfilestream; size:integer; p:pointer; filen:string;
v:variant;
begin
filen:=filename;
if filen='' then begin result:=error_nofilename; exit; end;
if not fileexists(filen) then begin result:=error_nofileexists; exit; end;
f:=Tfilestream.Create(filen,fmOpenRead);
try
v:=vararraycreate([0,f.size-1],varbyte);
p:=vararraylock(v);
try
size:=f.Read(p^,f.size);
finally f.Free; end;
vararrayunlock(v);
databuf:=v;
result:=size;
except
result:=error_readfile;
end;
end;
//客户端向服务器端的传送文件;filename:含相对路径的文件名;
//databuf:传送的文件内容;datalen:文件长度;overflag:有相同文件是否覆盖,>0覆盖;<=0不覆盖并返回错误;function TMonitor.sendfilebuf(databuf: OleVariant;
const filename: WideString; overflag: Integer): Integer;
var f:Tfilestream; size:integer; p:pointer; fullpath,filen:string;
begin
filen:=filename;
if filen='' then begin result:=error_nofilename; exit; end;
size:=VarArrayHighBound(databuf,1)-VarArrayLowBound(databuf,1)+1;
if size<=0 then begin result:=error_readfilebuf; exit; end;
fullpath:=ExtractFileDir(filen);
if not DirectoryExists(fullpath) then
if not ForceDirectories(fullpath) then begin result:=error_mkdir;exit; end;
if (fileexists(filen)) and (overflag<=0) then begin result:=error_writefileexists; exit; end;
p:=vararraylock(databuf);
vararrayunlock(databuf);
if fileexists(filen) then f:=Tfilestream.Create(filen,fmOpenWrite)
else f:=Tfilestream.Create(filen,fmCreate);
try
try
f.WriteBuffer(p^,size);
finally
f.Free;
end;
result:=size;
except
result:=error_writefile;
end;
end;这时服务端代码
上面是客户端
我用的dcom
你看看吧