PHP imports excel and displays the progress bar in the front desk
怪我咯2017-05-24 11:34:21
0
3
1294
php imports excel and the front desk displays a progress bar, please advise (not the progress of uploading excel files, but the progress bar of php reading excel->processing data->writing to mysql)
This depends on the size of your Excel and the maximum time allowed by the server for one request. I think it can be divided into the following situations:
Excel is very small, and the import is completed in a second or two - in this case, it is enough to just display a fake progress bar. For example: the progress of the animation in the first second reaches 50%, the progress in the second second reaches 75%, the progress of the third animation reaches 90%, and then the progress reaches 100% after the server returns OK.
Excel is relatively large, but it can be completed within the maximum time allowed by the server for a request -- In this case, there are several approaches:
To be simple and crude, estimate the progress of importing into Excel and store it in a certain location (such as a field in the database). Make another ajax request or EventSource to poll the progress every period of time (such as 500ms) and display it. animation.
Use HTTP long connection to output the progress in the request to import Excel. Remember to refresh the browser in time. The browser displays a progress animation based on the returned data.
Excel is very large or the business is complex and cannot be completed within the maximum time allowed by the server for a request--At this time, if the server can run a CLI script, it is recommended to run a CLI script to import (for progress display, please refer to 2.1) ; Otherwise, you have to divide Excel into steps/time/sections, and then perform the import task step by step in the browser, and display the progress according to the divided results.
Can I get the total number of records after reading excel? Yes, then just count the number of records processed/total number. No, then give up the 100% completion progress bar and just make a progress bar that loops repeatedly to show that I am still alive.
I don’t know if you can use swoole, that is to say, after you upload excel, get the number of records, and then directly use swoole to import sql. As long as you count the number of records in the database for your current page, you can get the progress percentage.
This depends on the size of your Excel and the maximum time allowed by the server for one request. I think it can be divided into the following situations:
Excel is very small, and the import is completed in a second or two - in this case, it is enough to just display a fake progress bar. For example: the progress of the animation in the first second reaches 50%, the progress in the second second reaches 75%, the progress of the third animation reaches 90%, and then the progress reaches 100% after the server returns OK.
Excel is relatively large, but it can be completed within the maximum time allowed by the server for a request -- In this case, there are several approaches:
To be simple and crude, estimate the progress of importing into Excel and store it in a certain location (such as a field in the database). Make another ajax request or EventSource to poll the progress every period of time (such as 500ms) and display it. animation.
Use HTTP long connection to output the progress in the request to import Excel. Remember to refresh the browser in time. The browser displays a progress animation based on the returned data.
Excel is very large or the business is complex and cannot be completed within the maximum time allowed by the server for a request--At this time, if the server can run a CLI script, it is recommended to run a CLI script to import (for progress display, please refer to 2.1) ; Otherwise, you have to divide Excel into steps/time/sections, and then perform the import task step by step in the browser, and display the progress according to the divided results.
Can I get the total number of records after reading excel?
Yes, then just count the number of records processed/total number.
No, then give up the 100% completion progress bar and just make a progress bar that loops repeatedly to show that I am still alive.
I don’t know if you can use swoole, that is to say, after you upload excel, get the number of records, and then directly use swoole to import sql. As long as you count the number of records in the database for your current page, you can get the progress percentage.