在 php 脚本准备下载文件时添加加载栏
我是 php 的新手,但成功创建了一个名为“welcome.php”的页面,该页面从表单中获取您的名字和姓氏,然后将它们放入一个简单的文本文件中并将其发送回给您下载:
<?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>
什么我想做的是在生成此文本文件时添加一个进度条,当弹出“保存文件”对话框时达到 100%。我知道在这种情况下加载时间非常短,但我稍后会对其进行修改以制作更大的文本文件。我想知道您是否可以为我指明正确的方向或提供几行代码来帮助我,因为我只学习了 php 两天。我相信您可以使用称为“刷新”的东西,但我不确定如何将其集成到我的页面中。谢谢!
请注意,我的页面也有一个小故障,文本文件还输出表单的 html,有谁知道如何阻止这种情况发生?
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 happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实上,PHP并没有提供这样的功能。
要显示进度条,您可以尝试以下操作:
在执行长时间运行的文件生成过程时,在当前用户会话中保存完成百分比值
在显示下载的页面上表单,添加一个 iFrame调用 progress.php 脚本
progress.php 脚本需要生成网页,需要自动重新加载
progress.php 脚本需要从会话中读取当前进度值并且绘制进度条图形,直观地表示进度。
虽然这是一个仅 PHP 的解决方案,您也可以使用 JS / AJAX 准备另一个解决方案。
HTML 自动重新加载应该像这样:
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:
您可以使用 http://jqueryui.com/demos/progressbar/ 它是基于 JavaScript 的,您可以正如 Stefan 所说,通过 AJAX 发送您的进度。
You could use http://jqueryui.com/demos/progressbar/ it is JavaScript based and you can send your progress via AJAX, like Stefan said.