file.vue 3.1 KB

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