0
votes

i need a function in a datasnap-server, which returns a .zip file. So i started with:

function TGetData.getZip (): TFileStream;
begin
   result := TFileStream.Create('test.zip', fmOpenRead and fmShareDenyWrite);
end

This works fine, but datasnap doesn't free it, so i get a memory leak error.

Next try: I started at "TWebModule1.WebModuleAfterDispatch". I thought it could help to send my response with "response.SendResponse;" and free my stream on my own. So here a short version:

procedure TWebModule1.WebModuleAfterDispatch(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
   test : TFileStream;
begin
   response.ContentType := 'application/x-zip-compressed';
   test := TFileStream.Create('test.zip', fmOpenRead);
   response.ContentStream := test;
   response.CustomHeaders.Values['Content-Disposition'] := 'attachment; filename=test12.zip';
   response.SendResponse; //Also sendStream didn't help
   test.Free;
end;

This is nearly the solution i think, but the datasnap-server sends html-code after my stream was sent and this is written at the end of the file.

I know, the datasnap-server doenst support TFileStream. Instead i should use TDBXStreamValue, but there seems to be no working example...

Has any one expirience with such a problem?

1
What HTML is being written at the end of the file? - Remy Lebeau
the html part contains the response-informations like reposnse code (in example 200), the content type and so on. - ChessDev
That is not HTML, that is HTTP, and I find it very unlikely that such data would ever be getting written to the end of the file. That would imply that DataSnap is not detecting that SendResponse() has already been called and is trying to send a second response. As for TFileStream, the documentation specifically states that a TFileStream can be used for the ContentStream, so why would you think it is not supported? Because it is not being freed? That would be a different bug. For instance, have you tried setting response.FreeContentStream := true; yet? - Remy Lebeau

1 Answers

0
votes

Thank you @remy lebeau

response.FreeContentStream := true;

this is the solution.

My function:

function TGetData.articleZippedImages(skip, take: Integer; since: string) : TStream;

the function does not free the stream!

procedure TWebModule1.WebModuleAfterDispatch(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
   response.FreeContentStream := true;
   Response.Content := '';
   Response.CustomHeaders.Values['Content-Disposition'] := 'attachment; filename=test.zip';
   Response.SendResponse;
end;