取消长时间运行的下载 [英] Cancel long running download

查看:87
本文介绍了取消长时间运行的下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在NGINX上有一个使用PHP服务器端语言的Web应用程序.通过JS,用户可以从非常慢的数据库中下载由PHP动态构建的文件.因此,接收第一个字节进行打印大约需要20秒.然后,更多数据随后将连续流出.

I have a web application with a PHP server side language on NGINX. Via JS, a user is able to download a file which is constructed dynamically by PHP from a database that is very slow. Therefore, to receive the first bytes for printing takes about 20 seconds. Then, more data is continuously streamed out afterwards.

PHP:

header("Content-Disposition: attachment; filename=\"" . $filename . ".txt\"");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Set-Cookie: fileDownload=" . $filename . "; path=/");   

while (Stream::MORE_DATA == $client->receive($data)) {
   print $data;
   flush();
}

此下载是通过iframe进行的(出于其他原因,它必须保留为iframe):

This download is taking place via an iframe (and it has to stay as an iframe for other reasons):

iframe = document.createElement("iframe");
iframe.style.display = "none";
iframe.src = downloadFileUrl;
(document.getElementsByTagName("body")[0]).appendChild(iframe);

如果用户想导航到新窗口中的另一个页面,则在下载完成之前不会加载另一个页面,因为NGINX一次只处理一个请求(我认为).因此,我为用户提供了一种取消下载的方式:

In the case the user wants to navigate to another page in a new window, the other page will not load until the download is complete because NGINX is only serving up one request at a time (I think). So I enabled a way for the user to cancel the download:

iframe.contentWindow.stop();
iframe.parentNode.removeChild(iframe);

如果在打印文件的第一部分之前的最初20秒钟取消下载,此操作将成功进行.但是,即使在首次打印后取消该文件,即使iframe无效,该文件仍会继续下载.

This works successfully if the download is cancelled in that initial 20 seconds before the first part of the file printed. However, if cancelled after the first print, the file still continues to be downloaded even though the iframe is nullified.

除了通过Internet浏览器的下载管理器手动取消下载之外,还有其他方法可以完全中止下载吗?

也许可以通过JavaScript,PHP或NGINX配置更改来解决?谢谢!

Maybe this can be solved with JavaScript, PHP or an NGINX config change? Thanks!

推荐答案

因此,我仍然无法找到使用JavaScript取消浏览器下载管理器中已启动的下载的方法.但是,在session_start之后使用session_write_close可以避免这些并发问题,因此,可以同时运行多个PHP脚本(即,当用户在其他窗口中浏览页面时,仍然可以进行下载)

So I still have not been able to figure out a way with JavaScript to cancel an already initiated download that appears in the download manager of a browser. But, using session_write_close after session_start will allow these concurrency issues to be avoided, and therefore, multiple PHP scripts to be ran simultaneuosly (i.e. the download can still occur while a user is navigating the page in a different window)

header("Content-Disposition: attachment; filename=\"" . $filename . ".txt\"");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Set-Cookie: fileDownload=" . $filename . "; path=/");   

session_write_close();
while (Stream::MORE_DATA == $client->receive($data)) {
   print $data;
   flush();
}

这篇关于取消长时间运行的下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆