js下载篇
1、地址下载
// 地址下载,fileName暂无作用 export const urlDownload = (url, fileName = '下载文件') => { // 创建隐藏的可下载链接 let eleLink = document.createElement('a') eleLink.download = fileName eleLink.style.display = 'none' eleLink.href = url // 触发点击 document.body.appendChild(eleLink) eleLink.click() // 然后移除 document.body.removeChild(eleLink) }
2、转化text下载
// 内容转化为文件下载 export const fileDownload = (file, fileName = '下载文件') => { // 创建隐藏的可下载链接 let eleLink = document.createElement('a') eleLink.download = fileName eleLink.style.display = 'none' // 字符内容转变成blob地址 let blob = new Blob([file]) eleLink.href = URL.createObjectURL(blob) // 触发点击 document.body.appendChild(eleLink) eleLink.click() // 然后移除 document.body.removeChild(eleLink) }