1.frame标签-js
const downloadFile = (url) => {
const frame = document.createElement(`iframe`)
frame.src = url
frame.style.display = `none`
document.body.appendChild(frame)
setTimeout(function () {
frame.remove()
}, 5 * 60 * 1000)
}
2.a标签-html
<a
:href="url"
target="_blank"
>
下载
</a>
3.a标签-js
let fileUrl = ''
let aLink = document.createElement('a')
aLink.style.display = 'none'
aLink.href = fileUrl
aLink.setAttribute('download', fileName)
document.body.appendChild(aLink)
aLink.click()
document.body.removeChild(aLink)
4.调用接口-blob
const instance = axios.create({
baseURL: "http://192.168.43.55:8000",
timeout: 5000,
responseType: 'blob'
});
this.axios.post(url, param).then(res => {
let blob = new Blob([res.data]);
let blobUrl = window.URL.createObjectURL(blob);
let aLink = document.createElement('a');
aLink.style.display = 'none';
aLink.href = blobUrl;
aLink.setAttribute('download', fileName)
document.body.appendChild(aLink);
aLink.click();
document.body.removeChild(aLink);
window.URL.revokeObjectURL(blobUrl)
})
.catch(e => console.log(e))
导出excel格式: new Blob([res], {type: 'application/vnd.ms-excel'})
导出word格式: new Blob([res], {type: 'application/msword'})
导出.zip格式: new Blob([res], {type: 'application/zip'})
导出.xls或者.xlsx new Blob([res], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'})