37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
// 下载blob文件
|
|
export const downloadFile = ({ fileData, fileType, fileName }) => {
|
|
const blob = new Blob([fileData], {
|
|
type: fileType
|
|
})
|
|
const link = document.createElement('a')
|
|
link.href = URL.createObjectURL(blob)
|
|
link.download = fileName
|
|
link.style.display = 'none'
|
|
document.body.appendChild(link)
|
|
link.click()
|
|
URL.revokeObjectURL(link.href)
|
|
document.body.removeChild(link)
|
|
}
|
|
|
|
// 通用a链接下载
|
|
export const downloadFileByUrl = (url) => {
|
|
const link = document.createElement('a');
|
|
link.href = url; // 设置文件 URL
|
|
link.download = ''; // 提供下载提示
|
|
document.body.appendChild(link); // 将链接添加到 DOM
|
|
link.click(); // 模拟点击下载
|
|
document.body.removeChild(link); // 下载后移除链接
|
|
}
|
|
|
|
// pdf、doc、docx等文件下载
|
|
export const downloadFileData = ({ fileName, fileUrl }) => {
|
|
const link = document.createElement('a')
|
|
link.setAttribute('download', '')
|
|
link.style.display = 'none'
|
|
link.href = fileUrl
|
|
link.download = fileName
|
|
document.body.appendChild(link)
|
|
link.click();
|
|
// URL.revokeObjectURL(link.href)
|
|
document.body.removeChild(link)
|
|
} |