merdj
merdj

Reputation: 19

delphi DataSnap 2010 Stream File working Sample

I am trying to stream an XML File from Server To Client using DataSnap, with the help of ldsandon, i was able to download the sample from embarcadero, but my problem is I cannot follow it.

a pseudo of the program should work this way.

client will request from the server for selected xml file in the combobox. the server will load the client selected xml file back to client.

i am just am trying to figure it out using delphi DataSnap, if not I will either use synapse or indy for tranferring the file, but I found Datasnap to be interesting.

could anyone help me please, a working if possible?

thanks a lot.

Please Help me, I need your help very badly.. thanks and thanks

I found this link, but I could not figure out how to convert it to TFileStream

Upvotes: 1

Views: 1702

Answers (1)

Freddie Bell
Freddie Bell

Reputation: 2287

// server side

function TServerMethods1.GetCDSXML(SQL: String; var FileSize: Integer): TStream;
begin

  QryMisc.Close;
  QryMisc.SQL.Text := SQL;
  CDSMisc.Open;
  Result := TMemoryStream.Create;
  try
    CDSMisc.SaveToStream(Result, dfXML);
    FileSize := Result.Size; // not CDSMisc.DataSize;
    Result.Position := 0; // Seek not implemented in abstract class
  finally
    CDSMisc.Close;
  end;

end;

// client side

procedure TClientModule1.PopMiscCDS(SQL: String);
const
   BufSize = $8000;
var
   RetStream: TStream;
   Buffer: PByte;
   MemStream: TMemoryStream;
   BytesRead: Integer;
   FileSize: Integer;
begin
   try
     MemStream := TMemoryStream.Create;
     GetMem(Buffer, BufSize);
     try
       //---------------------------------------------------------
       RetStream := ServerMethods1Client.GetCDSXML(SQL, FileSize);
       //---------------------------------------------------------
       repeat
         BytesRead := RetStream.Read(Pointer(Buffer)^, BufSize);
         if BytesRead > 0 then
           MemStream.WriteBuffer(Pointer(Buffer)^, BytesRead);
       until BytesRead < BufSize;

       if FileSize <> MemStream.Size then
         raise Exception.Create('Error downloading xml');

       MemStream.Seek(0, TSeekOrigin.soBeginning);
       CDSMisc.Close;
       CDSMisc.LoadFromStream(MemStream);

     finally
       FreeMem(Buffer, BufSize);
       MemStream.Free;
     end;

   except
     on E: Exception do
     begin
        ShowMessage(E.Message);
     end;
   end;

end;

Upvotes: -1

Related Questions