简体   繁体   English

我如何知道从 URL 下载完成的时间?

[英]How do I know when download from a URL is completed?

On my project I use something like the following function to redirect users in order to download a file在我的项目中,我使用以下 function 来重定向用户以下载文件

function promptDownload(file){
      location.href = "http://example.com/downloads/"+file;
}

As we all know, when I call this function, browser only prompts a download dialog and does not interrupt my application flow.众所周知,当我调用这个function时,浏览器只会提示一个下载对话框,并不会中断我的应用流程。 What I would like to do is to determine when this download completed or cancelled.我想做的是确定此下载何时完成或取消。

There should be something like onLoad , onFinishedLoading , onConnectionEnd or etc but I couldn't find anything.应该有类似onLoadonFinishedLoadingonConnectionEnd等的东西,但我找不到任何东西。

You can't determine download progress if you download the file that way.如果您以这种方式下载文件,则无法确定下载进度。

If you download the file using XMLHttpRequest , then you can listen for load, error and progress events like this:如果您使用XMLHttpRequest下载文件,那么您可以像这样监听加载、错误和进度事件:

function log(message) {
  return function () {
    alert(message);
  };
}

function download(file, callback) {
  var request = new XMLHttpRequest();
  request.responseType = 'blob';
  request.open('GET', file);
  request.addEventListener('load', log('load ' + file));
  request.addEventListener('error', log('error ' + file));
  request.addEventListener('progress', log('progress ' + file));
  request.addEventListener('load', function () {
    callback(request.response);
  });
  request.send();
}

function save(object, mime, name) {
  var a = document.createElement('a');
  var url = URL.createObjectURL(object);
  a.href = url;
  a.download = name;
  a.click();
}

document.querySelector('#download').addEventListener('click', function () {
  download('test.pdf', function (file) {
    save(file, 'application/pdf', 'test.pdf');
  });
});
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
  </head>
  <body>
    <button id="download">Download</button>
    <script src="script.js"></script>
  </body>
</html>

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

相关问题 我怎么知道ajax请求何时完成 - How do I know when an ajax request is completed 您如何知道从JavaScript开始下载的时间? - How do you know when a download has started from JavaScript? 如何知道请求已经下载完成? - how to know request already download completed? 我如何知道一组异步功能是否已完成执行? - How do I know if a group of async functions have completed executing? 如何从外部网址下载文件到变量? - How do I download a file from external url to variable? 如何知道jQuery函数何时完成 - How to know when a function jQuery has completed 如果我知道它将以什么名称存储,如何从 firebase 存储文件中获取图像下载 url? - How can I get image download url from firebase storage file, if I know what name it will be stored under? 当我知道所有异步调用都已完成时,如何运行回调? - How can I run a callback when I know all asynchronous calls have been completed? 我如何知道生成 CKEditor WYSIWYG 编辑器的 javascripts 何时完成执行? - How can I know when the javascripts that generate the CKEditor WYSIWYG editors have completed their execution? Flux体系结构-如何从动作中了解异步事件何时完成 - Flux architecture - how to know in controller when async event from action is completed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM