Vue实现下载文件

vue后台项目实现下载功能

1.首先封装后台接口downFile.js

export function downFile(id: any) {
    return service({
        url: `/compliance/filecheck/getFile/?id=${id}`,
        method: 'get',
        responseType: 'blob',
    })
}

2.页面的话直接绑定

<el-button @click="down(scope.row)" :icon="Download"  type="info" size="small">下载源文件</el-button>

3.直接上逻辑

1.1 页面引入封装好的接口

import { downFile } from '/@/api/downFile';

注意:vue3的引入方式可能会跟vue2有所不同.

//下载
		const down = (scope: any) => {
			console.log(scope)
			console.log(scope.id)
			const filName = scope.fileName;
			downFile(scope.id).then((res) => {
				console.log(res);
				const data = res;
				let url = window.URL.createObjectURL(new Blob([data]));
				let link = document.createElement('a');
				link.style.display = 'none';
				link.href = url;
				link.setAttribute('download', filName);
				document.body.appendChild(link);
				link.click();
			});
		};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值