H5下载文件跨域问题解决方案

const downloadRes = async (url, name) => {
    let response = await fetch(url)
    // 内容转变成blob地址
    let blob = await response.blob()
    // 创建隐藏的可下载链接
    let objectUrl = window.URL.createObjectURL(blob)
    let a = document.createElement('a')
    //地址
    a.href = objectUrl
    //修改文件名
    a.download = name
    // 触发点击
    document.body.appendChild(a)
    a.click()
    //移除
    setTimeout(() => document.body.removeChild(a), 1000)
}
let url = 'https://pics4.baidu.com/feed/241f95cad1c8a7869584e9de81872b3472cf50c8.jpeg?token=4c44645e8c72bc8e0e0fcac40cd27e77'
downloadRes(url,'名称')

 

posted @ 2022-02-14 17:53  前进中的蜗牛  阅读(532)  评论(0编辑  收藏  举报