12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- /**
- * 使用a链接下载文件,使用promise封装
- */
- export function downloadFileByA(url: string, name: string) {
- try {
- const link = document.createElement('a')
- link.href = url
- link.download = name
- document.body.appendChild(link)
- link.click()
- document.body.removeChild(link)
- }
- catch (error) {
- console.log(error)
- }
- }
- /**
- * 下载文件
- * @returns
- */
- export async function downloadFile(url: string, name: string) {
- try {
- const response = await fetch(url)
- if (!response.ok)
- throw new Error('下载失败')
- const blob = await response.blob()
- const link = document.createElement('a')
- link.href = URL.createObjectURL(blob)
- link.download = name
- document.body.appendChild(link)
- link.click()
- document.body.removeChild(link)
- }
- catch (error) {
- console.error(error)
- }
- }
- /**
- * 下载文件
- * @returns
- */
- export async function downloadFileA(url: string, name: string) {
- try {
- const link = document.createElement('a')
- link.href = url
- link.download = name
- document.body.appendChild(link)
- link.click()
- document.body.removeChild(link)
- }
- catch (error) {
- console.log(error)
- }
- }
|