下载功能相比大家已经不陌生了吧
这都是基本常规操作啦!直接上代码
一. 文件下载
1. 先写好下载的方法
export default function getDownloadData(content, name) {
const blob = new Blob([content])
if ('download' in document.createElement('a')) {
// 非IE下载
const elink = document.createElement('a')
elink.download = name
elink.style.display = 'none'
elink.href = window.URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL对象
document.body.removeChild(elink)
} else {
// IE10+ 下载
navigator.MsSaveBlob(blob, name)
}
}
2.在页面中使用
<div @click="clickDownloadFile(item)">下载</div>
import {downloadFile} from '@/api/api.js' // 引入下载接口
import download from '@/utils/download.js' // 引入写好的下载方法