知道文件的下载路径后,执行文件下载操作,支持 pdf、pdf、word、excel、png等资源。
filename 为传入的文件名、filePath 为资源路径
downItem(filename, filePath) {
let link
if (filePath.includes('pdf') || filePath.includes('png')|| filePath.includes('jpg') ||filePath.includes('jpeg')) {
link = document.createElement('a')
link.href = `${filePath}`
link.setAttribute('download', filename)
link.click()
URL.revokeObjectURL(filePath)
}else {
link = document.createElement('iframe')
link.style.display = 'none'
link.src = `${filePath}`
}
document.body.appendChild(link)
setTimeout(() => {
document.body.removeChild(link)
}, 500)
},
}