SoftTimur
SoftTimur

Reputation: 5550

Progress bar for an application

I have implemented a SAAS scenario with my Windows server: a user could upload a file in a website, then Fetch.exe is an application coded in C# hosted in the server, Fetch.exe takes the uploaded file as an input, executes and generates an output to download for the user.

So in my php, I use exec to wrap Fetch.exe:

exec("Fetch.exe " . $inputFile . " > " . $outputFile)

Uploading and executing (ie, Fetch.exe) may take more than several seconds, and I want to show the user that it is processing and everything is going fine.

I have found some threads that discuss how to show a progress bar for the uploading. Whereas, does anyone know what I could do to show the approximate progress of Fetch.exe? Do I have to split it into smaller applications and use several exec?

Upvotes: 1

Views: 182

Answers (2)

Cesar
Cesar

Reputation: 717

Your PHP program needs a way to know the state of the subprocess (the Fetch.exe application), so, Fetch.exe needs to send info about the processing state, the most natural way to do this is through the standard output (the standard output is the information that provides a program when you run it from cmd).

Knowing this, you can run and keep reading a subprocess output from php using popen().

And secod, you can use the PHP ob_flush() and flush() with the onmessage javascript event to establish the comunication from your client page with your running php script, here you can find a good tutorial on how do this.

Upvotes: 1

WesleyE
WesleyE

Reputation: 1384

You could supply Fetch.exe with an randomly generated ID from php, for example from the uniqueid function. Fetch.exe will create a file called <uniqueid>.txt with a progress percentage. From the browser, you could call another script with that unique ID to get the contents of that .txt file. In order, it would be something like this:

  • User uploads the file to PHP
  • PHP:
    • handles the uploaded file
    • creates a uniqueID
    • starts Fetch.exe with the file and the uniqueID
    • returns a page with the uniqueID embedded

The following happens in parallel:

  • Fetch.exe creates a textfile called /progress/uniqueid.txt with the uniqueid as name. It logs the progress into it.
  • The browser does an AJAX call to http://example.com/progress/uniqueid.txt and shows the progress to the user

And finally, when the progress reaches 100% the browser downloads the file. The only thing you might want to add is the pruning of the progress files after a while. Say you delete all files older than 10 minutes every hour.

Upvotes: 2

Related Questions