<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>前端项目下载导出文件</title>
</head>
<body>
<script>
/**
* post 方式
* 返回:文件流
* 好处:可以自己修改文件名称 方便调试
*/
let params ={
ListData : this.ListData
}
_this.$http.post(url,params,{responseType:"arraybuffer"} //必须添加项
).then(function(res) {
console.log(res)
var blob = new Blob([res.data], {type: 'application/msword;charset=utf-8'});
var filename = "download.doc";
var a = document.createElement('a');
var url = window.URL.createObjectURL(blob);
a.href = url;
a.download = filename;
var body = document.getElementsByTagName('body')[0];
body.appendChild(a);
a.click();
body.removeChild(a);
window.URL.revokeObjectURL(url);
}
/**
* get 方式
* 返回:文件流
* 好处:前台什么都不需要处理 完全后台处理
* 缺点:不变调试(需要确保后台接口稳定)
*/
let exportURL = `api/sysLog/export?content=${content}&ip=${ip}`;
window.open(exportURL, "_blank")
</script>
</body>
</html>