开发者

Progress bar updates from a PHP Script

开发者 https://www.devze.com 2023-02-09 03:05 出处:网络
I\'m writing a small app that does the following: Allows a user to upload an excel file Reads the data from each column of the excel file and creates an image based on the data

I'm writing a small app that does the following:

  1. Allows a user to upload an excel file
  2. Reads the data from each column of the excel file and creates an image based on the data

Right 开发者_如何转开发now I upload the files and call the script that cycles through the file, makes the images, create a page (or db entry) with each pic name, and then displays them on screen. I would PREFER to have it notify the use of it's progress though. I would like for the PHP script which makes the images to be called via ajax (I know how to do that), but for it to essentially return a value JSON,HTML, plain text, doesn't really matter.

Is this doable? I'm using JQuery, so maybe there is a feature in it's AJAX calls I don't know about?


Yes, it is doable. It is not a jQuery feature since it requires server-side integration. Here is a rough sketch on how to implement it:

  1. Have a database table (or other equivalent kinds of storage) that stores key-value pairs. It is simplest if both the keys and the values are strings.
  2. When the job is initiated, generate a unique string which is the key and value 0 which you store in the database table. The user is also redirected to an URL that contains that unique string. Eg. "/mysite/ERxQl3ew".
  3. Using jQuery, the users client should poll the url "/getkeyvalue/ERxQl3ew". This url should check the database and get the job status, say "45.3" to indicate that the job is 45.3% done. It's simplest if the page is a JSON string.
  4. The server should update the row with the key ERxQl3ew whenever the status or progress of that job changes.
  5. When the job is completed, the server could set the rows value to "final" to indicate that the job is done. When the client javascript sees that value, it should redirect the user to the final destination page or otherwise indicate to the user that the job is done.

Maybe it sounds more complicated than it really is. It is fairly trivial to implement.


i dont think so. If you would like to have the progress checked, you would have to check via jquery, i dont know, every 2 seconds. the problem is, you would have to run the php script asynchronously and reporting internaly the progress, which you would ask for via javascript. It´s not possible that you upload some stuff, and than, without any doing, your php scripts answers you something


I recommend the following:

  1. you have to make some progress meter, for example a file, in which you write how far the formatting process went sofar
  2. you also need a php, that reads that file and tells what precentage it read from it
  3. you need to call this php with ajax periodically until it reads 100%

Also dont forget to use some token to manage concurrent usage.


jQuery has a progress bar, and also there are a couple PHP scripts that returns the "progress" which outputs the status of the script. Here is a blog that i came across awhile back. Alternative PHP Cache (APC) method as well as the UploadProgress method. Both of them are PECL packages. hope this helps

howto-php-and-jquery-upload-progress-bar


It's doable.

There are a few ways to do this really, but one very simple way is to have a database table that tracks imports. When a row is processed, update the database table, then have JavaScript call a PHP script every few seconds via Ajax and it can return the total rows and processed rows, or a percentage etc.

The schema could be something totally simple like this:

Import ID | Total Rows | Processed Rows
---------------------------------------
100       | 12125      | 1246
101       | 6212       | 1302
0

精彩评论

暂无评论...
验证码 换一张
取 消