#1 2016-05-13 10:25:59

StxLog
Member
From: France
Registered: 2015-09-14
Posts: 58

Returning file within a TMVCApplication

Hi! I already have asked similar question but didn't had the time to try everything out at this time, so here i am again.

In my TMVCApplication, I'd like a function (or procedure doesn't matter) to return a pdf from stream.
So after search and try, I'm with

function Test(const id: Integer): TServiceCustomAnswer;
begin
    [...]
    Result.Header := BINARY_CONTENT_TYPE_HEADER;
    Result.Content := StreamToRawByteString(TMemoryStream(pdfExport.Stream));    
end;

From what I understand, doesn't this should be enough? But when I link to Test?id=22 well it redirect me to Test.html... (and create a mustache template)

I also have tried with CurrentServiceContext() and is Request member, couldn't figure out how to do it.

If someone could point me in the right direction I would greatly appreciate.

Offline

#2 2016-05-13 10:59:58

Chaa
Member
Registered: 2011-03-26
Posts: 249

Re: Returning file within a TMVCApplication

You can register separate service method for downloading files.

procedure TMyApp.Start(aRestModel: TSQLRest);
begin
  inherited Start(aRestModel, TypeInfo(IMyApp));
  ...
  TSQLRestServer(aRestModel).ServiceMethodRegister('DownloadFile', DownloadFile, True);
end;

procedure TMyApp.DownloadFile(Ctxt: TSQLRestServerURIContext);
begin
  // If you need authentification
  if CurrentSession.CheckAndRetrieve(@LSessionData, TypeInfo(TCookieData)) <> 0 then
  begin
    Ctxt.Returns(...);
  end
  else
    Ctxt.Error('', HTML_FORBIDDEN);
end;

Offline

#3 2016-05-13 14:05:13

StxLog
Member
From: France
Registered: 2015-09-14
Posts: 58

Re: Returning file within a TMVCApplication

Thanks for your reply and your time ! It worked as expected.

Offline

Board footer

Powered by FluxBB