vben
authored
5 years ago
1
<template>
vben
authored
5 years ago
2
<PageWrapper title="文件下载示例">
vben
authored
5 years ago
3
4
5
6
7
<a-alert message="根据后台接口文件流下载" />
<a-button type="primary" class="my-4" @click="handleDownByData"> 文件流下载 </a-button>
<a-alert message="根据文件地址下载文件" />
<a-button type="primary" class="my-4" @click="handleDownloadByUrl"> 文件地址下载 </a-button>
vben
authored
5 years ago
8
9
10
<a-alert message="base64流下载" />
<a-button type="primary" class="my-4" @click="handleDownloadByBase64"> base64流下载 </a-button>
vben
authored
5 years ago
11
12
13
14
15
<a-alert message="图片Url下载,如果有跨域问题,需要处理图片跨域" />
<a-button type="primary" class="my-4" @click="handleDownloadByOnlineUrl">
图片Url下载
</a-button>
vben
authored
5 years ago
16
</PageWrapper>
vben
authored
5 years ago
17
18
19
</template>
<script lang="ts">
import { defineComponent } from 'vue';
vben
authored
5 years ago
20
21
22
23
24
25
import {
downloadByUrl,
downloadByData,
downloadByBase64,
downloadByOnlineUrl,
} from '/@/utils/file/download';
vben
authored
5 years ago
26
import imgBase64 from './imgBase64';
vben
authored
5 years ago
27
import { PageWrapper } from '/@/components/Page';
vben
authored
4 years ago
28
import { Alert } from 'ant-design-vue';
vben
authored
5 years ago
29
vben
authored
5 years ago
30
export default defineComponent({
vben
authored
4 years ago
31
components: { PageWrapper, [Alert.name]: Alert },
vben
authored
5 years ago
32
33
34
35
36
37
38
39
40
setup() {
function handleDownByData() {
downloadByData('text content', 'testName.txt');
}
function handleDownloadByUrl() {
downloadByUrl({
url: 'https://codeload.github.com/anncwb/vue-vben-admin-doc/zip/master',
target: '_self',
});
vben
authored
5 years ago
41
42
43
44
45
downloadByUrl({
url: 'https://vebn.oss-cn-beijing.aliyuncs.com/vben/logo.png',
target: '_self',
});
vben
authored
5 years ago
46
}
vben
authored
5 years ago
47
48
49
50
function handleDownloadByBase64() {
downloadByBase64(imgBase64, 'logo.png');
}
vben
authored
5 years ago
51
52
53
54
function handleDownloadByOnlineUrl() {
downloadByOnlineUrl(
'https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5944817f47b8408e9f1442ece49d68ca~tplv-k3u1fbpfcp-watermark.image',
vben
authored
4 years ago
55
'logo.png',
vben
authored
5 years ago
56
57
);
}
vben
authored
5 years ago
58
59
60
return {
handleDownloadByUrl,
handleDownByData,
vben
authored
5 years ago
61
handleDownloadByBase64,
vben
authored
5 years ago
62
handleDownloadByOnlineUrl,
vben
authored
5 years ago
63
64
65
66
};
},
});
</script>