Copy ResourceStream In Stream

245 Views Asked by At

I need help in this code

This is all code

Request: TIdHTTPRequestInfo

Response: TIdHTTPResponseInfo

JQuery: TResourceStream

procedure TServer.ActiveServer;
begin
  DefaultPort := 8117;
  Active := True;
  OnCommandGet := FServerCommandGet;
  JQuery := TResourceStream.Create(HInstance,'JQuery',RT_RCDATA);
end;

function TServer.FindFiles: Boolean;
var
  Stream : TStream;
begin
  if Request.Document = '/jquery.js' then
  begin
    Response.ContentType := 'application/x-javascript';
    Stream := TStream.Create;
    Stream.Position := 0;
    Stream.CopyFrom(JQuery,JQuery.Size);
    Response.ContentStream := Stream;
    Result := True;
  end else
    Result := False;
end;

error : TStream.Seek not implemented

The problem is in the copy TResourceStream in TStream

1

There are 1 best solutions below

10
whosrdaddy On

TStream is an abstract class so it cannot be used directly, you must use one it's descendants. TResourceStream for example.

P.S : I see that you are dealing with Indy, no need to free the stream when assigned to ContentStream, Indy will handle that for you.

function TServer.FindFiles: Boolean;
var
  Stream : TResourceStream;
begin
  if Request.Document = '/jquery.js' then
  begin
    Response.ContentType := 'application/x-javascript';
    Response.ContentStream := TResourceStream.Create(HInstance, 'JQuery', RT_RCDATA);
    Result := True;
  end else
  begin
    Result := False;
  end;
end;