file.vue
3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<template>
<view>
<page-head :title="title"></page-head>
<view class="uni-padding-wrap uni-common-mt">
<block v-if="tempFilePath">
<image :src="tempFilePath" class="image" mode="aspectFit"></image>
</block>
<block v-if="!tempFilePath && savedFilePath">
<image :src="savedFilePath" class="image" mode="aspectFit"></image>
</block>
<block v-if="!tempFilePath && !savedFilePath">
<view class="uni-hello-addfile" @click="chooseImage">+ 请选择文件</view>
</block>
<view class="uni-btn-v">
<button class="btn-savefile" @click="saveFile">保存文件</button>
<button @click="clear">删除文件</button>
</view>
<!-- #ifndef MP-ALIPAY || MP-TOUTIAO -->
<view class="btn-area">
<button @click="openDocument">打开pdf文件</button>
</view>
<!-- #endif -->
</view>
</view>
</template>
<script>
export default {
data() {
return {
title: 'saveFile',
tempFilePath: '',
savedFilePath: ''
}
},
onLoad() {
this.savedFilePath = uni.getStorageSync('savedFilePath');
},
methods: {
chooseImage() {
uni.chooseImage({
count: 1,
success: (res) => {
this.tempFilePath = res.tempFilePaths[0];
},
fail: (err) => {
// #ifdef MP
uni.getSetting({
success: (res) => {
let authStatus = res.authSetting['scope.album'] && res.authSetting['scope.camera'];
if (!authStatus) {
uni.showModal({
title: '授权失败',
content: 'Hello uni-app需要从您的相机或相册获取图片,请在设置界面打开相关权限',
success: (res) => {
if (res.confirm) {
uni.openSetting()
}
}
})
}
}
})
// #endif
}
});
},
saveFile() {
if (this.tempFilePath.length > 0) {
uni.saveFile({
tempFilePath: this.tempFilePath,
success: (res) => {
this.savedFilePath = res.savedFilePath;
uni.setStorageSync('savedFilePath', res.savedFilePath);
uni.showModal({
title: '保存成功',
content: '下次进入页面时,此文件仍可用',
showCancel: false
});
},
fail: (res) => {
uni.showModal({
title: '保存失败',
content: '失败原因: ' + JSON.stringify(res),
showCancel: false
});
}
})
} else {
uni.showModal({
content: '请选择文件',
showCancel: false
});
}
},
clear() {
uni.setStorageSync('savedFilePath', '');
this.tempFilePath = '';
this.savedFilePath = '';
},
// #ifndef MP-ALIPAY || MP-TOUTIAO
openDocument() {
uni.downloadFile({
url: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/b3f1d0b0-5168-11eb-bd01-97bc1429a9ff.pdf',
success: (res) => {
uni.openDocument({
filePath: res.tempFilePath,
success: () => {
console.log('打开文档成功');
}
});
}
});
},
// #endif
}
}
</script>
<style>
.image {
width: 100%;
height: 360rpx;
}
.btn-savefile {
background-color: #007aff;
color: #ffffff;
}
</style>