Jump to content
uniGUI Discussion Forums
jahlxx

send file from a thread

Recommended Posts

Hi.

 

I have created a report, using FR, a everything works fine. Execute the queries, and export to pdf.

 

But this don't work and I don't know how to solve it:

      UniSession.SendFile(UniServerModule.StartPath + 'files\tmp\' + xreport,xreport);
 

 

Some help please.

 

Thanks.

Share this post


Link to post
Share on other sites

Hi.

 

If I understood you correctly:

function TUniReport.ExportReport: Boolean;
var Exp1: TfrxPDFExport;
    Exp2: TfrxJPEGExport;
    AUrl : string;
    P: Integer;
    OldFName, NewFName: String;
begin
  Result:=False;
  case FExportType of
    retPDF:
      begin
        Exp1:=TfrxPDFExport.Create(nil);
        try
          Exp1.Background := True;
          Exp1.ShowProgress := False;
          Exp1.ShowDialog := False;
-- Get a new file name in the cache !!!
          Exp1.FileName := UniServerModule.NewCacheFileUrl(False, 'pdf', '', '', AUrl, True);
          Exp1.DefaultPath := '';
          FRF.Export(Exp1);
-- Now display this file in a special form to view the PDF
          frm_UReport_Preview.UniURLFrame1.URL:=AUrl;
          frm_UReport_Preview.ShowModal;
          frm_UReport_Preview.Caption:=FRepName;
          Result:=True;
        finally
          Exp1.Free;
        end;
      end;

If you want to transfer the file to the client instead of displaying it:

  UniSession.SendFile(Exp1.FileName, SendAsFileName);
 

Share this post


Link to post
Share on other sites

no.

 

I have the report, and the pdf generated. all in a separated thread.

 

but unisession sendfile don't work. I don't how to do it when the thread ends.

 

 

Enviado desde mi SM-A500FU mediante Tapatalk

Share this post


Link to post
Share on other sites

hi.

 

I've tested with a unitimer. Launch the report in a separated thread, and enable the timer. In the ontimer event, I control if the thread is terminated or not. If yes, disable the timer and sendfile. This work fine, but only in standalone (unitimer is for client side).

 

Now, trying to use unithreadtimer.

 

Launch the report, and do this:

 

        unithreadtimer1.enabled := true;
 

 

And this:

 

   if UniThread1.terminated then begin
      UniSession.SendFile(UniServerModule.StartPath + 'files\tmp\' + xreport2,xreport2);
      unithreadtimer1.enabled := false;
   end;
 

 

This should work, but don't work.

 

Any idea?

 

Thanks.

Share this post


Link to post
Share on other sites

Hi.

 

I forgot one detail.

 

I use delphi standar class TThread to generate de report (execute queries and export to pdf).

 

My problem is send file to the user once the pdf is finished.

 

I don't know how to do it.

 

Does anyone know how to do?

 

I'm locked with this.

 

Thanks.

Share this post


Link to post
Share on other sites

OK.

 

I've solved this. With UniTimer.

 

My last test, and works in standalone and isapi is this:

 

procedure TForm1.UniTimer1Timer(Sender: TObject);
begin
   try
      UniSession.SendFile(UniServerModule.StartPath + 'files\tmp\' + xreport2, xreport2);
      form1.unitimer1.enabled := false;
   except
      form1.unitimer1.enabled := true;
   end;
end;

 

I don't know if is the best solution, but works for me as expected and as I need.

 

If someone has a better solution and/or any suggestion, it will be wellcome.

 

Thanks.

 

Share this post


Link to post
Share on other sites

The pdf is probably not ready to be sent, so the sendfile fails.

 

Try and do a loop, checking FileExists/sleep if not, and wait to send until it exists,

and see if that was the issue.

Share this post


Link to post
Share on other sites

Ok.

 

With FileExists, allways returns true, because the pdf file exists. Is not ready, but exists while is generated.

 

I do this, and I think is correct:

 

      if rpt.Export(exp) then begin
         assignfile(fic, UniServerModule.StartPath + '\files\tmp\flag.txt');
         Rewrite(fic);
         closefile(fic);
      end;
 

And in unitimer on time event:

 

   if FileExists(UniServerModule.StartPath + '\files\tmp\flag.txt') then begin
      UniSession.SendFile(UniServerModule.StartPath + 'files\tmp\' + xreport2+'.pdf', xreport2+'.pdf');
      flstrendimientos.unitimer1.enabled := false;
      try
         deletefile(UniServerModule.StartPath + '\files\tmp\flag.txt');
      except
      end;
      xreport2 := '';
   end;
 

 

Generate a small txt file, when the export ends.

 

This works for me.

 

Thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×