使用 PHP 和 JavaScript 下载状态

发布于 2025-01-04 21:29:49 字数 271 浏览 1 评论 0 原文

我目前正在研究一种在页面上显示文件下载状态的方法。 我知道这是不需要的,因为用户通常在浏览器中具有下载状态,但我想让用户保持在他正在下载的页面上,只要下载是持久的。为此,下载状态应与文件实际状态匹配(而不是假进度条)。也许它还会显示用户下载的速度,并根据当前的下载速率估计所需的时间。

这可以使用 PHP 和 Javascript 来完成吗?或者它真的需要 Flash 或 Java 吗?

服务器上的某个地方不应该有关于谁正在以什么速度和多少下载什么的信息吗?

提前感谢您的帮助。

I'm currently looking into a way of showing the file download status on a page.
I know this isnt needed since the user usually has a download status in the browser, but I would like to keep the user on the page he is downloading from, as long as the download is lasting. To do that, the download status should match the status the file actually has (not a fake prograss bar). Maybe it will also display the speed the user is downloading at, and estimate the time it will take, depending on the current download rate.

Can this be done using PHP and Javascript? Or does it realy require Flash or Java?

Should not somewhere on the Server be an information about who is downloading what at what speed and how much?

Thank you for your help in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏璃陌 2025-01-11 21:29:49

不太可能跨浏览器,但请查看 http://markmail.org/message/kmrpk7w3h56tidxs#query:jquery%20ajax%20download%20progress+page:1+mid:kmrpk7w3h56tidxs+state:results 进行了相当密切的努力。 IE(一如既往)是不玩球的罪魁祸首。

Not really possible cross-browser, but have a look into http://markmail.org/message/kmrpk7w3h56tidxs#query:jquery%20ajax%20download%20progress+page:1+mid:kmrpk7w3h56tidxs+state:results for a pretty close effort. IE (as usual) is the main culprit for not playing ball.

╰◇生如夏花灿烂 2025-01-11 21:29:49

您可以使用两个单独的 php 文件来完成此操作,第一个文件用于下载过程。
就像这样:

$strtTime=time();
$download_rate=120;   //downloading rate  
    $fp = fopen($real, "r");
      flush();// Flush headers
    while (!feof($fp)) {   
     $downloaded=round($download_rate * 1024);
        echo fread($fp,$downloaded );
        ob_flush();
       flush(); 
        if (connection_aborted ()) {

     // unlink("yourtempFile.txt" ;
            exit;
        }

     $totalDw +=$downloaded;
     // file_put_contents("yourtempFile.txt", "downloaded: $totalDw ; StartTime:$strtTime");
          sleep(1);
    }
    fclose($fp);
   // unlink("yourtempFile.txt") ;

第二个文件将用于通过 Ajax 持续读取 yourtempFile.txt 。由于开始打印,因此不会使用会话和 Cookie。

You can do it with two seperate php files, first file for downloading process.
Like as:

$strtTime=time();
$download_rate=120;   //downloading rate  
    $fp = fopen($real, "r");
      flush();// Flush headers
    while (!feof($fp)) {   
     $downloaded=round($download_rate * 1024);
        echo fread($fp,$downloaded );
        ob_flush();
       flush(); 
        if (connection_aborted ()) {

     // unlink("yourtempFile.txt" ;
            exit;
        }

     $totalDw +=$downloaded;
     // file_put_contents("yourtempFile.txt", "downloaded: $totalDw ; StartTime:$strtTime");
          sleep(1);
    }
    fclose($fp);
   // unlink("yourtempFile.txt") ;

Second file would be used for reading yourtempFile.txt by Ajax continusly. Using Sessions and Cookies wouldn't be used because of starting print.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文