Hi Guys.
Hi Guys.
Can anybody confirm if this is the right way to send a stream in an ISAPI app?
I am a little bit irritated because it seams not to be working by just calling SendStream alone ...
In a standalone app everything works fine like this ....
pprocedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
ms: TMemoryStream;
png: TPNGImage;
begin
if not Response.Sent then
begin
try
ms := TMemoryStream.Create;
png := TPNGImage.Create;
png.LoadFromFile('JESUISCHARLIE.png');
png.SaveToStream(ms);
Response.ContentType := 'image/png';
Response.SendStream(ms);
Handled := true;
finally
png.Free;
end;
end;
end;
... but in the ISAPI dll I seem to have to do it like this ...
procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
ms: TMemoryStream;
png: TPNGImage;
begin
if not Response.Sent then
begin
try
ms := TMemoryStream.Create;
png := TPNGImage.Create;
png.LoadFromFile('JESUISCHARLIE.png');
png.SaveToStream(ms);
Response.ContentType := 'image/png';
Response.SendResponse;
ms.Position := 0;
Response.SendStream(ms);
Handled := true;
finally
png.Free;
end;
end;
end;
So two questions:-
1.
Why do I need the sendresponse and the ms.Position := 0 in the ISAPI scenario ?
2.
Not freeing the ms is correct if I remember right because it gets freed after being send to the client - right?
Can anybody confirm if this is the right way to send a stream in an ISAPI app?
I am a little bit irritated because it seams not to be working by just calling SendStream alone ...
In a standalone app everything works fine like this ....
pprocedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
ms: TMemoryStream;
png: TPNGImage;
begin
if not Response.Sent then
begin
try
ms := TMemoryStream.Create;
png := TPNGImage.Create;
png.LoadFromFile('JESUISCHARLIE.png');
png.SaveToStream(ms);
Response.ContentType := 'image/png';
Response.SendStream(ms);
Handled := true;
finally
png.Free;
end;
end;
end;
... but in the ISAPI dll I seem to have to do it like this ...
procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
ms: TMemoryStream;
png: TPNGImage;
begin
if not Response.Sent then
begin
try
ms := TMemoryStream.Create;
png := TPNGImage.Create;
png.LoadFromFile('JESUISCHARLIE.png');
png.SaveToStream(ms);
Response.ContentType := 'image/png';
Response.SendResponse;
ms.Position := 0;
Response.SendStream(ms);
Handled := true;
finally
png.Free;
end;
end;
end;
So two questions:-
1.
Why do I need the sendresponse and the ms.Position := 0 in the ISAPI scenario ?
2.
Not freeing the ms is correct if I remember right because it gets freed after being send to the client - right?
David Heffernan Hi - I get now why I did not understand you ... I was refering to another method in my code ... you are of course absolutely right on using a filestream in the case of this thread (and also the wrong try location :-)...
ReplyDeleteHow could I prevent the memory stream in this code - any idea?
...
procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
codetype: string;
scalestring: string;
ms: TMemoryStream;
begin
if not Response.Sent then
begin
bc.InputText := Request.QueryFields.Values['id'];
scalestring := Request.QueryFields.Values['scale'];
if scalestring <> '' then
begin
bc.Scale := StrToFloat(scalestring);
end;
codetype := Request.QueryFields.Values['codetype'];
if codetype <> '' then
begin
bc.SymbologyName := codetype;
end;
ms := TMemoryStream.Create;
bc.Bitmap.SaveToStream(ms);
Response.ContentType := 'image/png';
{$IFDEF ISAPI}
Response.SendResponse;
ms.Position := 0;
{$ENDIF}
Response.SendStream(ms);
Handled := true;
end;
end;
bc is the barcode component I was referring to before.
Please excorcise me on this :-)
I think you need a stream for this. Memory stream is fine here.
ReplyDeleteDavid Heffernan Ok. Thanks for caring and hinting the wrong try finally.
ReplyDelete