|
1 | 1 | <template> |
2 | | - <view> |
3 | | - <page-head :title="title"></page-head> |
| 2 | + <view> |
| 3 | + <page-head :title="title"></page-head> |
4 | 4 | <view class="uni-padding-wrap uni-common-mt"> |
5 | | - <block v-if="tempFilePath != ''"> |
| 5 | + <block v-if="tempFilePath"> |
6 | 6 | <image :src="tempFilePath" class="image" mode="aspectFit"></image> |
7 | 7 | </block> |
8 | | - <block v-if="tempFilePath === '' && savedFilePath != ''"> |
| 8 | + <block v-if="!tempFilePath && savedFilePath"> |
9 | 9 | <image :src="savedFilePath" class="image" mode="aspectFit"></image> |
10 | 10 | </block> |
11 | | - <block v-if="tempFilePath === '' && savedFilePath === ''"> |
12 | | - <view class="uni-hello-addfile" @tap="chooseImage">+ 请选择文件</view> |
| 11 | + <block v-if="!tempFilePath && !savedFilePath"> |
| 12 | + <view class="uni-hello-addfile" @click="chooseImage">+ 请选择文件</view> |
13 | 13 | </block> |
14 | 14 | <view class="uni-btn-v"> |
15 | | - <button class="btn-savefile" @tap="saveFile">保存文件</button> |
16 | | - <button @tap="clear">删除文件</button> |
| 15 | + <button class="btn-savefile" @click="saveFile">保存文件</button> |
| 16 | + <button @click="clear">删除文件</button> |
17 | 17 | </view> |
18 | 18 | <view class="btn-area"> |
19 | | - <button @tap="openDocument">打开pdf文件</button> |
| 19 | + <button @click="openDocument">打开pdf文件</button> |
20 | 20 | </view> |
21 | 21 | </view> |
22 | | - </view> |
| 22 | + </view> |
23 | 23 | </template> |
24 | 24 | <script> |
25 | | - export default { |
26 | | - data() { |
27 | | - return { |
28 | | - title: 'saveFile', |
29 | | - tempFilePath: '', |
30 | | - savedFilePath: '' |
31 | | - } |
32 | | - }, |
33 | | - onLoad() { |
34 | | - this.savedFilePath = uni.getStorageSync('savedFilePath') |
35 | | - }, |
36 | | - onUnload() { |
37 | | - this.tempFilePath = "", |
38 | | - this.savedFilePath = "" |
39 | | - }, |
40 | | - methods: { |
41 | | - chooseImage() { |
42 | | - uni.chooseImage({ |
43 | | - count: 1, |
44 | | - success: (res) => { |
45 | | - this.tempFilePath = res.tempFilePaths[0] |
46 | | - } |
47 | | - }) |
48 | | - }, |
49 | | - saveFile() { |
50 | | - if (this.tempFilePath.length > 0) { |
51 | | - uni.saveFile({ |
52 | | - tempFilePath: this.tempFilePath, |
53 | | - success: (res) => { |
54 | | - this.savedFilePath = res.savedFilePath |
55 | | - uni.setStorageSync('savedFilePath', res.savedFilePath) |
56 | | - uni.showModal({ |
57 | | - title: '保存成功', |
58 | | - content: '下次进入页面时,此文件仍可用', |
59 | | - showCancel: false |
60 | | - }) |
61 | | - }, |
62 | | - fail: (res) => { |
63 | | - uni.showModal({ |
64 | | - title: '保存失败', |
65 | | - content: '失败原因: ' + JSON.stringify(res), |
66 | | - showCancel: false |
67 | | - }) |
68 | | - } |
69 | | - }) |
70 | | - } else { |
71 | | - uni.showModal({ |
72 | | - content: "请选择文件", |
73 | | - showCancel: false |
74 | | - }) |
75 | | - } |
76 | | - }, |
77 | | - clear() { |
78 | | - uni.setStorageSync('savedFilePath', '') |
79 | | - this.tempFilePath = '', |
80 | | - this.savedFilePath = '' |
81 | | - }, |
82 | | - openDocument() { |
83 | | - uni.downloadFile({ |
84 | | - url: 'https://raw.githubusercontent.com/mozilla/pdf.js/master/examples/learning/helloworld.pdf', |
85 | | - success: function (res) { |
86 | | - var filePath = res.tempFilePath |
87 | | - uni.openDocument({ |
88 | | - filePath: filePath, |
89 | | - success: function (res) { |
90 | | - console.log('打开文档成功') |
91 | | - } |
92 | | - }) |
93 | | - } |
94 | | - }) |
95 | | - } |
96 | | - } |
97 | | - } |
| 25 | + export default { |
| 26 | + data() { |
| 27 | + return { |
| 28 | + title: 'saveFile', |
| 29 | + tempFilePath: '', |
| 30 | + savedFilePath: '' |
| 31 | + } |
| 32 | + }, |
| 33 | + onLoad() { |
| 34 | + this.savedFilePath = uni.getStorageSync('savedFilePath'); |
| 35 | + }, |
| 36 | + methods: { |
| 37 | + chooseImage() { |
| 38 | + uni.chooseImage({ |
| 39 | + count: 1, |
| 40 | + success: (res) => { |
| 41 | + this.tempFilePath = res.tempFilePaths[0] |
| 42 | + } |
| 43 | + }) |
| 44 | + }, |
| 45 | + saveFile() { |
| 46 | + if (this.tempFilePath.length > 0) { |
| 47 | + uni.saveFile({ |
| 48 | + tempFilePath: this.tempFilePath, |
| 49 | + success: (res) => { |
| 50 | + this.savedFilePath = res.savedFilePath |
| 51 | + uni.setStorageSync('savedFilePath', res.savedFilePath) |
| 52 | + uni.showModal({ |
| 53 | + title: '保存成功', |
| 54 | + content: '下次进入页面时,此文件仍可用', |
| 55 | + showCancel: false |
| 56 | + }) |
| 57 | + }, |
| 58 | + fail: (res) => { |
| 59 | + uni.showModal({ |
| 60 | + title: '保存失败', |
| 61 | + content: '失败原因: ' + JSON.stringify(res), |
| 62 | + showCancel: false |
| 63 | + }) |
| 64 | + } |
| 65 | + }) |
| 66 | + } else { |
| 67 | + uni.showModal({ |
| 68 | + content: "请选择文件", |
| 69 | + showCancel: false |
| 70 | + }) |
| 71 | + } |
| 72 | + }, |
| 73 | + clear() { |
| 74 | + uni.setStorageSync('savedFilePath', '') |
| 75 | + this.tempFilePath = '', |
| 76 | + this.savedFilePath = '' |
| 77 | + }, |
| 78 | + openDocument() { |
| 79 | + uni.downloadFile({ |
| 80 | + url: 'https://raw.githubusercontent.com/mozilla/pdf.js/master/examples/learning/helloworld.pdf', |
| 81 | + success: function(res) { |
| 82 | + var filePath = res.tempFilePath |
| 83 | + uni.openDocument({ |
| 84 | + filePath: filePath, |
| 85 | + success: function(res) { |
| 86 | + console.log('打开文档成功') |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
98 | 94 | </script> |
99 | 95 |
|
100 | 96 | <style> |
101 | | - .image { |
102 | | - width: 100%; |
103 | | - height: 360upx; |
104 | | - } |
| 97 | + .image { |
| 98 | + width: 100%; |
| 99 | + height: 360upx; |
| 100 | + } |
105 | 101 |
|
106 | | - .btn-savefile { |
107 | | - background-color: #007aff; |
108 | | - color: #ffffff; |
109 | | - } |
110 | | -</style> |
| 102 | + .btn-savefile { |
| 103 | + background-color: #007aff; |
| 104 | + color: #ffffff; |
| 105 | + } |
| 106 | +</style> |
0 commit comments