open-document.uvue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <page-head :title="title"></page-head>
  3. <!-- #ifdef APP -->
  4. <scroll-view direction="vertical" style="flex:1">
  5. <!-- #endif -->
  6. <view class="uni-common-mt">
  7. <button v-for="(item, index) in fileList" :key="index" @click="openDocument(item)" style="margin: 10px;">
  8. 打开 {{item.type}} 文件
  9. </button>
  10. </view>
  11. <!-- #ifdef APP -->
  12. </scroll-view>
  13. <!-- #endif -->
  14. </template>
  15. <script setup lang="uts">
  16. type FileItem = {
  17. type : string,
  18. url : string
  19. }
  20. const title = 'openDocument'
  21. const fileList = ref<Array<FileItem>>([
  22. {
  23. type: 'pdf',
  24. url: 'https://web-assets.dcloud.net.cn/unidoc/zh/helloworld.pdf'
  25. },
  26. {
  27. type: 'doc',
  28. url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.doc'
  29. },
  30. {
  31. type: 'docx',
  32. url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.docx'
  33. },
  34. {
  35. type: 'ppt',
  36. url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.ppt'
  37. },
  38. {
  39. type: 'pptx',
  40. url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.pptx'
  41. },
  42. {
  43. type: 'xls',
  44. url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.xls'
  45. },
  46. {
  47. type: 'xlsx',
  48. url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.xlsx'
  49. },
  50. {
  51. type: 'zip',
  52. url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/to.zip'
  53. },
  54. {
  55. type: 'br',
  56. url: '/static/filemanager/1.txt.br'
  57. },
  58. {
  59. type: 'mp3',
  60. url: '/static/test-audio/ForElise.mp3'
  61. },
  62. {
  63. type: 'mp4',
  64. url: '/static/test-video/10second-demo.mp4'
  65. },
  66. {
  67. type: 'svg',
  68. url: '/static/test-image/logo.svg'
  69. }
  70. ])
  71. const openDocument = (item : FileItem) => {
  72. if (item.url.startsWith('http')) {
  73. uni.showLoading({
  74. title: '下载中',
  75. mask: true
  76. })
  77. uni.downloadFile({
  78. url: item.url,
  79. success: (res) => {
  80. uni.openDocument({
  81. filePath: res.tempFilePath,
  82. success: () => {
  83. uni.hideLoading()
  84. console.log('打开文档成功')
  85. },
  86. fail: (err) => {
  87. uni.hideLoading()
  88. console.log('打开文档失败', err)
  89. uni.showToast({
  90. title: '错误码:' + err.errCode.toString(),
  91. icon: "error"
  92. })
  93. }
  94. })
  95. },
  96. fail: (err) => {
  97. uni.hideLoading()
  98. console.log('下载失败', err)
  99. uni.showToast({
  100. title: '下载失败:' + err.errCode.toString(),
  101. icon: "error"
  102. })
  103. }
  104. })
  105. } else {
  106. uni.openDocument({
  107. filePath: item.url,
  108. success: () => {
  109. console.log('打开文档成功')
  110. },
  111. fail: (err) => {
  112. console.log('打开文档失败', err)
  113. uni.showToast({
  114. title: '错误码:' + err.errCode.toString(),
  115. icon: "error"
  116. })
  117. }
  118. })
  119. }
  120. }
  121. </script>