【问题标题】:javascript download file from pathjavascript从路径下载文件
【发布时间】:2020-11-20 06:35:38
【问题描述】:

以下是我用来下载文件的示例,但它只在下一个选项卡中打开文件。它不下载文件。

function download(items) {
 
  items.forEach(function (item, index) {
   // console.log(item);
    var anchor = document.createElement('a');
    anchor.href = item.url;
    anchor.target = '_blank';
    anchor.download = item.name;
    anchor.innerHTML ='download';
    setTimeout(function () {
    //  console.log(anchor);
      anchor.click();      
    }, index * 100);
  });
}

【问题讨论】:

标签: javascript html laravel file download


【解决方案1】:
fetch("https://upload.wikimedia.org/wikipedia/commons/a/a3/June_odd-eyed-cat.jpg")
    .then(response => response.blob())
    .then(blob => {

        const blobURL = URL.createObjectURL(blob);
        const a = document.createElement("a");
        a.href = blobURL;
        a.innerHTML = "download"
        a.setAttribute('download', 'true')
        document.body.appendChild(a);

    })

下载属性仅适用于同源网址

此 API 调用需要一个 URL 并创建一个可下载的图像

【讨论】:

  • 我现在收到“Access-Control-Allow-Origin”错误
猜你喜欢
  • 2021-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-09
  • 2021-11-06
  • 1970-01-01
  • 2018-11-09
  • 1970-01-01
相关资源
最近更新 更多