1.react实现简易的下载功能
1.如果对于某个接口来说,后端发给你的就是需要下载的文件内容自身。那么可以像下面这样实现下载:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| fetch(url, config).then(response => { if (response.status === 200) { response.blob().then(blobData => { let a = document.createElement('a'); let href = window.URL.createObjectURL(blobData); a.href = href; a.download = 'FILE_NAME'; document.body.appendChild(a).click(); window.URL.revokeObjectURL(href); }).catch(err => {
}) } }).catch(err => {})
|