js下载url文件 —— 三种方法

window.open

window.open(url, '_blank');

a标签

const ele = document.createElement('a'); //新建一个a标签
ele.setAttribute('href', url);
ele.click();

blob

fetch(url).then((res) =>
   res.blob().then((blob) => {
      var a = document.createElement('a');
      var url = window.URL.createObjectURL(blob);
      a.href = url;
      a.download = name; // 下载名称
      a.click();
      window.URL.revokeObjectURL(url);
    })
  );

你可能感兴趣的:(javascript,前端,html)