Jump to content

send file from a thread


jahlxx

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.

Link to comment
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);
 

Link to comment
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.

Link to comment
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.

Link to comment
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.

 

Link to comment
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.

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...