第一种方式
npm install --save js-file-download
<el-button size="mini" @click="exportExcel">下载</el-button>
import fileDownload from 'js-file-download';
exportExcel() {
var urls = `download`;
this.$confirm('确定导出?', '操作提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
}).then(() => {
axios({
url: urls,
method: "post",
responseType: "blob",
data: {
"Id": this.id,
"startTime": this.startTime,
"endTime": this.endTime
},
}).then((res) => {
console.log(res.data)
fileDownload("blob字节流", 'fileName')
fileDownload(res.data, "文件名称.xlsx");
this.$message({message: '下载成功!', type: 'success'});
})
}).catch(err => {
this.$message({type: 'info', message: '已取消'});
})
},
第二种方式
import axios from "axios";
axios({
url: `/docDownload`,
method: 'post',
responseType: 'blob',
dataType: 'json',
headers: { 'Content-type': 'application/json;' },
data: params
}).then((res) => {
const xlsx = 'application/vnd.ms-excel'
const blob = new Blob([res.data], { type: xlsx })
const a = document.createElement('a')
a.download = '周围文档列表' + new Date().getTime() + '.xlsx'
a.href = window.URL.createObjectURL(blob)
a.click()
a.remove()
})