admin 管理员组

文章数量: 1184232

download 属性是HTML5中新增的 <a> 标签属性。

  • 注意:
    • href属性的href 需要是 限于同源文件 ,如果引用的是第三方或是调用后台的接口,download就会不起作用
    • <a href={} target="_blank" download='文件名字'>点击下载</a>

  • 解决方法
  • 改为接口请求到文件流 本地重新创建
  •                const xhr = new XMLHttpRequest();
                    xhr.open('GET', url, true);  // url 文件的完整地址 http:XXX
                    xhr.responseType = 'blob';
                    xhr.onload = function () {
                      if (xhr.status === 200) {
                        const res = xhr.response;
                        const link = document.createElement('a');
                        link.style.display = 'none';
                        const url = window.URL.createObjectURL(res);
                        link.href = url;
                        link.setAttribute('download', `代码.xlsx`);
                        document.body.appendChild(link);
                        link.click();
                        window.URL.revokeObjectURL(link.href);
                        document.body.removeChild(link);
                      }
                    }
                    xhr.send()

本文标签: 难题 编程 破解技术