前端页面使用window.open("url")方式下载文件的时候,下载一个文件就会中断,不能一次性下载多个文件。

使用如下方法可解决:

先获取下载文件的 url 存到 files 数组中,然后多次调用 downloads(url)  方法即可。

let griddata = this.getAddinById('Grid1').getSelected()[0];
let files = JSON.parse(griddata.file);
console.log(files)
var count = 0;
files.map(file => {
    console.log(count++);
    downloads(file.url);
});
function downloads(url) {
  const iframe = document.createElement('iframe');
  iframe.style.display = 'none';
  function iframeLoad() {
    console.log('iframe onload');
    const win = iframe.contentWindow;
    const doc = win.document;
    if (win.location.href === url) {
      if (doc.body.childNodes.length > 0) {
         // response is error
      }
      iframe.parentNode.removeChild(iframe);
    }
  }
  if ('onload' in iframe) {
    iframe.onload = iframeLoad;
  } else if (iframe.attachEvent) {
    iframe.attachEvent('onload', iframeLoad);
  } else {
    iframe.onreadystatechange = function onreadystatechange() {
      if (iframe.readyState === 'complete') {
        iframeLoad;
      }
    };
  }
  iframe.src = '';
  document.body.appendChild(iframe)
  setTimeout(function loadUrl() {
    iframe.contentWindow.location.href = url;
  }, 50);
}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐