使用Javascript检查文件是否已下载

时间:2019-02-22 10:36:23

标签: javascript cookies browser download

正在处理一个具有下载文件链接的应用程序。我正在尝试使用Javascript创建功能,据此,我想立即检测到文件已下载,然后执行一些额外的任务。我已经通过下载文件进行了测试,但是它没有抛出警告框。

要点击的锚链接

<a href="{{ route('b2c.policy') }}" id="policyDownload" target="_blank"> Download Policy Document</a>

检测下载的逻辑

$( "a#policyDownload" ).mousedown(
    function(e) {
      //Get current time
      var downloadID = ( new Date() ).getTime();

      // Update the URL that is *currently being requested
      $( "a#policyDownload" ).href += ( "?downloadID=" + downloadID );

      //search for the name-value pattern with the above ID.
      var cookiePattern = new RegExp( ( "downloadID=" + downloadID ), "i" );

      //watch the local Cookies to see when the download ID has been updated by the response headers.
      var cookieTimer = setInterval( checkCookies, 500 );

      //check the local cookies for an update.
      function checkCookies() {
          // If the local cookies have been updated
          if ( document.cookie.search( cookiePattern ) >= 0 ) {
              clearInterval( cookieTimer );
              alert('Downloaded');
          }
      }
    }
  );

0 个答案:

没有答案