前端下载文件

前端创建下载窗口

export const downloadFile = (url, filename) => {
    const a = document.createElement('a')
    a.href = url
    a.download = filename
    document.body.appendChild(a)
    a.style = 'display: none'
    a.click()
    a.remove()
}

流文件下载

const fileName = `${row.appName}.txt`
exportGameParamAPI({ appId: row.id }).then(res => {
    let blob = new Blob([res])
    let url = URL.createObjectURL(blob)
    downloadFile(url, fileName)
})

接口

// 导出游戏参数
export const exportGameParamAPI = params => {
    return axios.get('/xxx/xxx', { params })
}

如果返回的是网址

downloadFile(url)

你可能感兴趣的:(前端下载文件)