简体   繁体   English

获取href上的请求状态

[英]Get Request Status on href

I am using a href link to download a file from the server. 我正在使用href链接从服务器下载文件。 I want to do some activity after the file downloaded completely from server. 我想在完全从服务器下载文件后做一些活动。 Like XMLHTTP, can i set some call back function on href which will be called on completion of the request. 与XMLHTTP一样,我可以在href上设置一些回调函数,该函数将在请求完成时调用。

My code is like this 我的代码是这样的

///link to download report
<div class='report-download'><a href='' id='downloadreq'></a></div>
$("#report-download").click(OnClick);

 ///onlick function
functon OnClick()
{
  ///start progress bar
  $("#downloadreq").attr("href","8001.html?data=dowloadreport");
  //stop progress bar

}

I want to show a progress bar while download happening and I have code to start and stop the progress bar window .But When i insert code to stop the Progress bar, its getting executed before actual download completed and the progress bar get disappeared. 我想在下载时显示进度条,我有代码来启动和停止进度条窗口。但是当我插入代码来停止进度条时,它会在实际下载完成之前执行并且进度条消失。 How will i identify the report download is completed (or) is it possible to identify that the download is completed. 如何确定报告下载已完成(或)是否可以确定下载已完成。 The time taken for report download will very depend on number of record to download. 报告下载所需的时间将取决于要下载的记录数。 So i cant wait for a default time period and stop the progress bar. 所以我不能等待默认时间段并停止进度条。

I think there is a no way like event to know if a file download has been already finished or not. 我认为,如果文件下载已经完成,我们就无法知道event If I were you, I would do something like this: 如果我是你,我会做这样的事情:

  1. Open the file via PHP and send it to the client's browser via HTTP 通过PHP打开文件并通过HTTP将其发送到客户端的浏览器
  2. I would have a variable like $_SESSION['lock'] = true; 我会有一个像$_SESSION['lock'] = true;的变量$_SESSION['lock'] = true; and will set it before start sending the stream to the output 并在开始将流发送到输出之前设置它
  3. When the file download got finished, I would unlock the variable by $_SESSION['lock'] = false; 文件下载完成后,我会通过$_SESSION['lock'] = false;解锁变量$_SESSION['lock'] = false;
  4. During the file download, I could check the lock value to see if the file has been finished or not. 在文件下载期间,我可以检查lock值以查看文件是否已完成。 Let say each 2-5 seconds via AJAX. 让我们通过AJAX每2-5秒说一次。
  5. When it's finished - lock is false , I can do whatever needed, but I have no idea how to show the progress bar, even approximately. 当它完成时 - lockfalse ,我可以做任何需要,但我不知道如何显示进度条,甚至大约。

Note that if you don't use something like a $_SESSION , the lock mechanism would not works at all, because the script would run each time as a fresh one, without having any idea of the recent variables, etc. 请注意,如果您不使用类似$_SESSION东西,则锁定机制根本不起作用,因为脚本每次都会作为一个新的运行,而不知道最近的变量等。

Update: 更新:

You may want to use $_SESSION['finished'] instead of $_SESSION['lock'] which would makes more sense, in terms of understanding the source code. 您可能想要使用$_SESSION['finished']而不是$_SESSION['lock'] ,这在理解源代码方面更有意义。

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

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