Delphi XE2 DataSnap - 通过TStream与进度条下载文件 [英] Delphi XE2 DataSnap - Download File via TStream With Progress Bar

查看:1038
本文介绍了Delphi XE2 DataSnap - 通过TStream与进度条下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个DataSnap服务器方法,返回一个TStream对象来传输一个文件。客户端应用程序调用该方法并读取流精细。我的问题是方法调用需要一段时间才能在TStream对象可用于读取之前完成,但是在服务器端,我可以看到方法调用只需要一秒钟来创建要返回的对象。我希望流对象将立即返回,以便我可以读取流并显示进度条以进行下载进度。还有另一种方式可以做到这一点吗?



服务器方法非常简单:

 code> function TServerMethods.DespatchDocument(sCompanyID,sDocOurRef:string):TStream; 
var
sSourceFilePath:string;
strFileStream:TFileStream;
begin
sSourceFilePath:= GetDocumentPDFFilePath(sCompanyID,sDocOurRef);

strFileStream:= TFileStream.Create(sSourceFilePath,fmOpenRead);
结果:= strFileStream;
结束


解决方案

这是我做了一会儿。我使用XE,没有机会清理它。



//服务器端:

 函数TServerMethods1.DownloadFile大小:Int64):TStream; 
begin
结果:= TFileStream.Create('upload.fil',fmOpenRead或fmShareDenyNone);
大小:= Result.Size;

Result.Position:= 0;
结束

//客户端:

  procedure TfMain.DownloadFile(Sender:TObject); 
var
RetStream:TStream;
缓冲区:PByte;
Mem:TMemoryStream;
BytesRead:Integer;
DocumentId:Int64;
大小: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';

重复
BytesRead:= RetStream.Read(指针(​​缓冲区)^,BufSize);

if(BytesRead> 0)then
begin
Mem.WriteBuffer(Pointer(Buffer)^,BytesRead);
结束

lStatus.Caption:= IntToStr(Mem.Size)+'/'+ IntToStr(Size);
Application.ProcessMessages;

until(BytesRead< BufSize);

if(Size<> Mem.Size)then
begin
raise Exception.Create('Error downloads file ...');
结束
end
else
begin
lStatus.Caption:='';
结束
finally
FreeMem(Buffer,BufSize);
FreeAndNIl(Mem);
结束
除了
在E:Exception do
begin
lErrorMessage.Caption:= PChar(E.ClassName +':'+ E.Message);
结束
结束
结束

您可以调整BufSize,但您喜欢。得到流的大小,直到我这样做才遇到麻烦。我试过XE2,似乎没有同样的问题,但我正在上传。可能有更好的方法来检索流的大小。如果我很快得到答案,我会让你知道....



另一个注意事项 - 我还没有弄清楚如何在服务器上显示一个进度条侧。我还在试图弄清楚这一点。



我希望这有帮助!如果您有任何疑问,请告知我!


I've written a DataSnap server method that returns a TStream object to transfer a file. The client application calls the method and reads the stream fine. My issue is that the method call takes a while to complete before the TStream object is available to read, but on the server side I can see that the method call only takes a second to create the object to return. I was hoping the stream object would be returned immediately so that I can read the stream and display a progress bar for the download progress. Is there another way I can do this?

The server method is very simple :

function TServerMethods.DespatchDocument(sCompanyID, sDocOurRef: string): TStream;
var
  sSourceFilePath: string;
  strFileStream: TFileStream;
begin
  sSourceFilePath := GetDocumentPDFFilePath(sCompanyID, sDocOurRef);

  strFileStream := TFileStream.Create(sSourceFilePath, fmOpenRead);
  Result := strFileStream;
end;

解决方案

This is how I did it a while back. I used XE and haven't had a chance to clean it up.

//Server side:

function TServerMethods1.DownloadFile(out Size: Int64): TStream;
begin
    Result := TFileStream.Create('upload.fil', fmOpenRead or fmShareDenyNone);
    Size := Result.Size;

    Result.Position := 0;
end;

//Client side:

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;

You can adjust BufSize however you like. I was having trouble getting the size of the stream until I did it this way. I experimented with XE2 and didn't seem to have the same problem but I was uploading. There is probably a better way to retrieve the size of the stream. If I get the answer soon I'll let you know....

On another note - I haven't figured out how to display a progress bar on the server side. I'm still trying to figure this out too.

I hope this helps! Let me know if you have any questions!

这篇关于Delphi XE2 DataSnap - 通过TStream与进度条下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆