Downloading a single file

  1. The simplest way
window.open(url, '_blank');
Copy the code
  1. Through the A tag
const a = document.createElement('a');
a.href = url; // File link
a.download = name; // File name, the cross-domain resource download is invalid
a.click();
a.remove();
Copy the code
  1. Using iframe
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.style.height = 0;
iframe.src = url;
// Unlike the previous two methods, iframe needs to be mounted to the page to trigger the request
document.body.appendChild(iframe);
setTimeout(() = > {
  iframe.remove();
}, 1000);
Copy the code

Download multiple files in batches

Bulk download is essentially multiple files downloaded at the same time, to a loop is done, but there are three ways to download, which should cycle?

1. Window.open looks the most neat and perfect, but when executed:

Only one or two files were successfully downloaded, and the rest were intercepted as malicious redirects

2. Come kangkang again the second way, through the A label download

  • For same-origin files, you can download them in batches by using the A label
  • For cross-domain files, again only one or two files were successfully downloaded, with the rest requesting canceled

3. Finally, iframe. Will it be the hope of the village?

It worked!! Only one file showed a security warning

First launched in: Guadi content creation platform