开发者

Adding a loading bar while php script is preparing a file for download

开发者 https://www.devze.com 2023-04-06 04:26 出处:网络
I\'m a newbie to php but have managed to create a page I call \"welcome.php\" that takes your first and last name from a form, then puts these in a simple text file and sends it back to you for downlo

I'm a newbie to php but have managed to create a page I call "welcome.php" that takes your first and last name from a form, then puts these in a simple text file and sends it back to you for download:

<?php 
if ($_POST) {

    header("Content-Type: application/txt");
    header('Content-Disposition: attachment; filename="welcome.txt"');
    echo "Welcome, ";
    echo $_POST['firstName']. " " . $_POST['lastName']; 

    // exit the script to make the form not appear in the downloaded file
    exit;
}

?>

<form action="" method="post">
First Name:  <input type="text" name="firstName" /><br />
Last Name: <input type="text" name="lastName" /><br />
<input type="submit" name="submit" value="Submit me!" />
</form>

What I want to do is to add a progress bar while this text file is being generated, which reaches 100% when the "save file" dialogue box pops up. I know in this case the load time is very short, but I will be modifying it later to make much larger text files. I was wondering if you could point me in the right direction or provide a couple of lines of code to help, bearing in my mind I've only been learning php for 2 days. I believe you can use something called flush, but I'm not sure how to integrate this into my page. Thanks!

Note that my page also has a slight glitch in that the text file also outputs the html of the form, does anyone know how to stop this hap开发者_运维问答pening?


Actually, PHP doesn't provide such a feature.

To show a progress-bar, you might try this:

  • While executing the long-running file generation process, save a percentage-done value in the current user session

  • On the page, which shows the download form, add an iFrame which calls a progress.php script

  • The progress.php script needs to generate webpage, which needs to reload auto-magically

  • The progress.php script needs to read the current progress-value from the session and draw a progress bar graphics, which visually represents the progress.

While this is a PHP-only solution, you might well prepare another one using JS / AJAX.

The HTML auto-reload should like like this:

<meta http-equiv="refresh" content="1; URL=http://myServer.com/progress.php">


You could use http://jqueryui.com/demos/progressbar/ it is JavaScript based and you can send your progress via AJAX, like Stefan said.

0

精彩评论

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

关注公众号