Skip to content

Instantly share code, notes, and snippets.

@p2yang p2yang/download.js

Last active Feb 12, 2020
Embed
What would you like to do?
原生 fetch download file
async function download (url = '', headers = {}) {
try {
const res = await fetch(url, {
headers
})
const blob = await res.blob()
// 获取后端headers里面的文件名
const filename = decodeURI(res.headers.get('Content-Disposition').split('filename=')[1])
// download
const a = document.createElement('a')
a.download = filename
a.style.display = 'none'
a.href = window.URL.createObjectURL(blob)
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
} catch (err) {
console.error(err)
// toast error message
}
}
@p2yang

This comment has been minimized.

Copy link
Owner Author

p2yang commented Feb 12, 2020

thanks for the issue comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.