#2 Re: mORMot 1 » MVC - serving binary file from stream » 2016-05-18 21:45:16

What type should I return in service method in order to return binary data with proper MIME declaration?

#3 mORMot 1 » MVC - serving binary file from stream » 2016-05-18 20:52:15

Virion
Replies: 4

Hello!

I am new to mORMot, implementing Angular2 application and serving it via mORMot MVC. I want to serve binary file (jpg, pdf etc.) from DB blob as a response in MVC action. I do not use ORM part of the framework, so let's just say I have a TStream with data in it. I've managed to build my own "MVCViews" - view engine:

procedure TMVCViewsBinary.Render(methodIndex: Integer; const Context: Variant;
  var View: TMVCView);
var
  lStream : TStringStream;
begin
  lStream := TStringStream.Create;
  FillStreamWithImageDataFromMyDatabase(lStream, Context);

  View.ContentType := 'image/jpeg';
  View.Content := lStream.DataString;

  lStream.Free;
end;

Works fine, but - stream loading logic placed in view engine doesn't look like a good idea. I've tried to return loaded stream object directly from MVC action, but all return params are serialized before passed to view engine - obviously, I don't want stream to be serialized on it's way, just 'rendered'.

Is there a better way to achieve my goal?

Board footer

Powered by FluxBB