** new Blob([data])用来创建URL的file对象或者blob对象**
关于下载在页面直接写url地址也是可以下载的,
window.location.href=“text.pdf”;
但后端接口有的时候要求传给我们前端的是文件流格式,前端处理方式如下
that.$http({
method:\"get\",
url:api.exportContract+\'/\'+agreementId+\'/\'+docNumber,
headers:headers(\'application/x-download\'),
responseType: \'blob\',
}).then(function(res){
let url = window.URL.create URL(new Blob([res.data])); // new Blob([data])用来创建URL的file对象或者blob对象
let = document.createElement(\'a\');
.style.display = \'none\';
.href = url;
.download= docNumber+\'.pdf\'; //docNumber 动态文件名
document.body.appendChild( );
.click();
});
Vue下载本地pdf文件
<button @click=\"downloadPDf\">downloadPDf</button>
downloadPDf() {
axios.post(\'http://localhost:8080/static/test.pdf\', {
responseType: \'blob\', //重要
}).then(response => {
const url = window.URL.create URL(new Blob([response.data]));
const = document.createElement(\'a\');
let fname = \'report.pdf\';
.href = url;
.setAttribute(\'download\', fname);
document.body.appendChild( );
.click();
})
}
}
记住本地文件一定要放在static文件夹下面。
继续阅读与本文标签相同的文章
上一篇 :
Android安卓进程保活
-
【从入门到放弃-ZooKeeper】ZooKeeper实战-分布式队列
2026-05-18栏目: 教程
-
Cassandra gossip介绍系列之一
2026-05-18栏目: 教程
-
GO学习笔记 - 数据校验
2026-05-18栏目: 教程
-
源码分析 RocketMQ DLedger 多副本之 Leader 选主
2026-05-18栏目: 教程
-
源码分析 RocketMQ DLedger 多副本存储实现
2026-05-18栏目: 教程
