index.vue 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <view>
  3. <button @click="previewFile">预览文件</button>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. return {
  10. }
  11. },
  12. methods: {
  13. previewFile() {
  14. uni.showLoading({
  15. title: '加载中'
  16. })
  17. uni.downloadFile({
  18. url: "http://114.115.142.166:8087/down/0OR9jtcjJHMj.xlsx", //后端返回的文件地址
  19. filePath: wx.env.USER_DATA_PATH + '/gwy.xls',
  20. // filePath: wx.env.USER_DATA_PATH + '/' + item.name + '.' + item.value.split('.')[item.value.split('.').length - 1],
  21. success: function(res) {
  22. if (res.statusCode === 200) {
  23. uni.openDocument({
  24. showMenu: true,
  25. filePath: res.filePath,
  26. success: function(res) {
  27. console.log(res, '打开文件成功')
  28. },
  29. fail: (err) => {
  30. uni.showToast({
  31. title: '打开文件失败请重试',
  32. icon: 'none'
  33. })
  34. }
  35. })
  36. } else {
  37. uni.showToast({
  38. title: '打开文件失败请重试',
  39. icon: 'none'
  40. })
  41. }
  42. uni.hideLoading()
  43. },
  44. fail: (err) => {
  45. uni.hideLoading()
  46. uni.showToast({
  47. title: '加载失败请重试',
  48. icon: "none"
  49. })
  50. }
  51. })
  52. }
  53. }
  54. }
  55. </script>
  56. <style>
  57. </style>