Jump to content


Photo

send file from a thread


  • Please log in to reply
7 replies to this topic

#1 jahlxx

jahlxx

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 862 posts

Posted 22 February 2018 - 12:44 PM

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.


  • 0

#2 Volk65

Volk65

    Active Member

  • uniGUI Subscriber
  • PipPipPip
  • 63 posts
  • LocationMSK, Russia

Posted 22 February 2018 - 03:39 PM

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);
 


  • 0

#3 jahlxx

jahlxx

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 862 posts

Posted 22 February 2018 - 05:45 PM

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
  • 0

#4 jahlxx

jahlxx

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 862 posts

Posted 23 February 2018 - 12:20 PM

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.


  • 0

#5 jahlxx

jahlxx

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 862 posts

Posted 26 February 2018 - 10:30 AM

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.


  • 0

#6 jahlxx

jahlxx

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 862 posts

Posted 26 February 2018 - 12:00 PM

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.

 


  • 0

#7 delphidude

delphidude

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 193 posts
  • LocationNorway

Posted 27 February 2018 - 11:15 PM

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.


  • 0

#8 jahlxx

jahlxx

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 862 posts

Posted 28 February 2018 - 11:24 AM

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.


  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users