紧急求助!下载文件问题 ( 积分: 50 )

  • 主题发起人 主题发起人 qinglan.ai
  • 开始时间 开始时间
Q

qinglan.ai

Unregistered / Unconfirmed
GUEST, unregistred user!
function DownloadFile(stSocket: TClientSocket; StrFileName: String): Boolean;
var
BinaryFile: THandle;
MiniBuffer: TMinBufferHeader;
MinExBuffer: TMinExBufferHeader;
lpBinaryBuffer: Pointer;
dwBytesRead, dwBytesDone, dwFileSize: DWORD;
begin
Result := False;
BinaryFile := CreateFile(Pchar(StrFileName), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if BinaryFile = INVALID_HANDLE_VALUE then Exit;
MinExBuffer.dwSocketCmd := File_DownLoadBegin;
dwFileSize := GetFileSize(BinaryFile, nil);
if dwFileSize = 0 then
begin
Result := True;
Exit;
end;
MinExBuffer.dwBufferSize := dwFileSize;
Sleep(1);
stSocket.SendBuffer(MinExBuffer, MinEx_BUFFER_SIZE);

// 等待发送指令
stSocket.ReceiveBuffer(MiniBuffer, MIN_BUFFER_SIZE);
if MiniBuffer.dwSocketCmd = File_DownLoadBegin then
begin
dwBytesDone := 0;
GetMem(lpBinaryBuffer, 4096);
try
while True do
begin
ReadFile(BinaryFile, lpBinaryBuffer^, 4096, dwBytesRead, nil);
// 读取文件成功
if (dwBytesRead > 0) then
begin
Sleep(10);
stSocket.SendBuffer(lpBinaryBuffer^, dwBytesRead);
Inc(dwBytesDone, dwBytesRead);
end else
begin
if dwBytesDone >= dwFileSize then
begin
Result := True;
Break;
end;
end;
end;
finally
FreeMem(lpBinaryBuffer);
end;
end;
CloseHandle(BinaryFile);
end;

大家帮忙看看这段代码有什么问题 ! 可以下载 可以每次最后几个字节都下载不行 这是什么原因呢?
 

Similar threads

后退
顶部