這是我做到了,而回。我使用XE並沒有機會清理它。
//服務器端:
function TServerMethods1.DownloadFile(out Size: Int64): TStream;
begin
Result := TFileStream.Create('upload.fil', fmOpenRead or fmShareDenyNone);
Size := Result.Size;
Result.Position := 0;
end;
//客戶端:
procedure TfMain.DownloadFile(Sender: TObject);
var
RetStream: TStream;
Buffer: PByte;
Mem: TMemoryStream;
BytesRead: Integer;
DocumentId: Int64;
Size: Int64;
filename: WideString;
BufSize: Integer;
begin
BufSize := 1024;
try
Mem := TMemoryStream.Create;
GetMem(Buffer, BufSize);
try
RetStream := FDownloadDS.DownloadFile(Size);
RetStream.Position := 0;
if (Size <> 0) then
begin
filename := 'download.fil';
repeat
BytesRead := RetStream.Read(Pointer(Buffer)^, BufSize);
if (BytesRead > 0) then
begin
Mem.WriteBuffer(Pointer(Buffer)^, BytesRead);
end;
lStatus.Caption := IntToStr(Mem.Size) + '/' + IntToStr(Size);
Application.ProcessMessages;
until (BytesRead < BufSize);
if (Size <> Mem.Size) then
begin
raise Exception.Create('Error downloading file...');
end;
end
else
begin
lStatus.Caption := '';
end;
finally
FreeMem(Buffer, BufSize);
FreeAndNIl(Mem);
end;
except
on E: Exception do
begin
lErrorMessage.Caption := PChar(E.ClassName + ': ' + E.Message);
end;
end;
end;
,只要你喜歡你可以調整BUFSIZE。在這樣做之前,我無法獲得流的大小。我用XE2進行了實驗,似乎沒有相同的問題,但我正在上傳。可能有更好的方法來檢索流的大小。如果我很快得到答案,我會讓你知道......
另一個說明 - 我還沒有想出如何顯示在服務器端的進度條。我仍然試圖弄清楚這一點。
我希望這有助於!如果您有任何問題,請告訴我!
可能重複://計算器。 com/questions/6081841/progressbar-for-tresourcestream-delphi) – NGLN 2012-02-27 03:38:29