简体   繁体   English

PHP-下载大文件时页面被阻止

[英]PHP - Page is blocked when large files are being downloaded

I have a download.php page which will be invoked everytime a user clicks "Download" button on my page. 我有一个download.php页面,每当用户单击我的页面上的“下载”按钮时,都会调用该页面。 The file is around 1 GB. 该文件约为1 GB。 However, the current page got blocked before the file is completely downloaded. 但是,当前页面在文件完全下载之前被阻止。 Is there a way I can make this an async task so that the user can still use the website while the file is being downloaded to his computer. 有没有一种方法可以使我执行异步任务,以便在文件下载到计算机上时用户仍然可以使用网站。

Thanks. 谢谢。

If you are using sessions, then before you begin any long running process you should call session_write_close() or you will get blocking site-wide. 如果使用会话,则在开始任何长时间运行的进程之前,应调用session_write_close(),否则将在整个站点范围内受到阻止。 From the docs there: 从那里的文档:

Session data is usually stored after your script terminated without the need to call session_write_close(), but as session data is locked to prevent concurrent writes only one script may operate on a session at any time. 会话数据通常在脚本终止后存储,而无需调用session_write_close(),但是由于会话数据被锁定以防止并发写入,因此任何时候任何会话都只能对一个脚本进行操作。

So, if you have a long running script that called session_start() and did not call session_write_close() then the result is, any page that needs to access a session now has to wait for the long running script to finish execution before it can begin. 因此,如果您有一个名为session_start()的长时间运行的脚本,而没有调用session_write_close()那么结果是,现在需要访问会话的任何页面都必须等待该长时间运行的脚本完成执行才能开始。 Thus, site-wide blocking. 因此,站点范围内的阻止。

This may or may not solve your issue, because I'm not sure what you mean by "blocking" in your question. 这可能会或可能不会解决您的问题,因为我不确定您在问题中“阻止”是什么意思。

First Create your download links like this :- 首先创建您的下载链接,如下所示:-

<a class="download" href="/myfiles/video/file.pdf"> Dow

nload file.pdf nload文件.pdf

then in Jquery : 然后在Jquery中:

$(document).ready(function(){

   $(document).on('.download', 'click', function(){

       var href = $(this).href;
       $('<iframe />', { src :  href }).appendTo('body');    
       return false; // prevent from default action, can be done with e.preventDefault() also

   }) // __download

}); // __ document ready

This technique use by many big sites like facebook, github etc, and it will not block current page, everything will be working like before but browser will show file save dialog to download file, I did not test this code so there may be some syntex error, Thanks :) 此技术已被许多大型网站(例如facebook,github等)使用,它不会阻止当前页面,所有功能都将像以前一样工作,但是浏览器将显示文件保存对话框以下载文件,我没有测试此代码,因此可能存在一些问题错误,谢谢:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM