38 lines
855 B
TypeScript
Raw Normal View History

/**
*
*/
/**
* URL
* @param url
* @param filename
*/
export const downloadFileFromUrl = (url: string, filename?: string) => {
try {
const link = document.createElement('a')
link.href = url
link.target = '_blank'
if (filename) {
link.download = filename
}
// 添加到DOM触发下载然后移除
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
} catch (error) {
console.error('下载文件失败:', error)
throw error
}
}
/**
*
* @param filename URL
*/
export const getFileExtension = (filename: string): string => {
const lastDotIndex = filename.lastIndexOf('.')
return lastDotIndex !== -1 ? filename.slice(lastDotIndex) : ''
}