JavaScript通过window.open(url)下载文件修改文件名
前端在页面上用window.open(filePath)下载服务器本地存储的文件,下载的名称为存储时的名称,若想要重新命名。用以下方法:
function downloadFile(filePath,name) {
const link = document.createElement('a')
link.style.display = 'none'
link.href = filePath
link.setAttribute(
'download',
name
)
document.body.appendChild(link)
link.click()
}
注:这种方式要是跨域的话就不行了,download属性只能在同源下才可以生效