HTTP文件下载:监控下载进度 [英] HTTP File Download: Monitoring Download Progress
问题描述
我在一个情况下,当我要实现的大型文件下载(最大支持4GB),从Web服务器:Apache通过HTTP协议2.4.4。我曾尝试多种方法,但最好的解决方案看起来为X-SENDFILE模块的使用。
I am in a situation, when I have to implement downloading of large files(up to 4GB) from a Web server: Apache 2.4.4 via HTTP protocol. I have tried several approaches, but the best solution looks to be the usage of X-SendFile module.
至于我提供文件上传进度条,我需要对文件下载相同的功能。因此,这里是我的问题:
As I offer progress bar for file uploads, I would need to have the same feature for file downloads. So here are my questions:
- 有什么办法,包括解决方法,实现文件下载进度监控?
- 有什么办法,包括解决方法,来计算文件下载传输速度?
- 有没有更好的办法,从除X-SENDFILE模块使用的Web服务器提供高效的文件下载?
有一般更好的文件下载选项,这将让我监视文件下载进度?它可以是在客户端(的JavaScript)或服务器溶液(PHP)。是否有允许这种任何特定的Web服务器?
Is there better file download option in general, that would allow me to monitor file download progress? It can be a client (JavaScript) or server solution(PHP). Is there any particular web server that allows this?
目前我使用的:
- 的Apache 2.4.4
- 的Ubuntu
很多次感谢。
推荐答案
2的想法(未验证):
2 ideas (not verified):
第一:
而不是将常规链接文件(要下载),像您的网页链接的地方...的/ dowanload.php这可能看上去某事像这样:
Instead of placing regular links to files (that you want to download) on your page place links like .../dowanload.php which may look sth like this:
<?php
// download.php file
session_start(); // if needed
$filename = $_GET['filename']);
header( 'Content-type: text/plain' ); // use any MIME you want here
header( 'Content-Disposition: attachment; filename="' . htmlspecialchars($filename) . '"' );
header( 'Pragma: no-cache' );
// of course add some error handling
$filename = 'c:/php/php.ini';
$handle = fopen($filename, 'rb');
// do not use file_get_contents as you've said files are up to 4GB - so read in chunks
while($chunk = fread($handle, 1000)) // chunk size may depend on your filesize
{
echo $chunk;
flush();
// write progress info to the DB, or session variable in order to update progress bar
}
fclose($handle);
?>
这样就可以在保持关注您的下载过程。在此期间,你可能会写入进度信息的DB /会话var和更新进度条当然使用轮询AJAX脚本来读取进度信息从DB /会话VAR读取状态。
This way you may keep eye on your download process. In the meantime you may write progress info to the DB/session var and update progress bar reading status from DB/session var using AJAX of course polling a script that reads progress info.
这是非常简单的,但我认为,只要你想它可能工作。
That is very simplified but I think it might work as you want.
二:
阿帕奇2.4内置了Lua的语言:
Apache 2.4 has Lua language built in:
- mod_lua
- 创建具有mod_lua 挂钩和脚本
- mod_lua
- Creating hooks and scripts with mod_lua
我敢打赌,你可以尝试写LUA Apache的处理程序,将监视您的下载 - 使用PHP / AJAX采取从DB进度信息发送进步到数据库和更新进度条
I bet you can try to write LUA Apache handler that will monitor your download - send progress to the DB and update progress bar using PHP/AJAX taking progress info from the DB.
相似 - 有Perl模块,甚至蟒蛇(而不是赢)
Similarly - there are modules for perl and even python (but not for win)
这篇关于HTTP文件下载:监控下载进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!