index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // pages/download/index.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. text: '下载文件'
  8. },
  9. backUrl() {
  10. wx.navigateBack({
  11. delta: 1
  12. })
  13. },
  14. // 预览文件
  15. lookFileFu(savedFilePath, that) {
  16. wx.openDocument({ //微信打开文件
  17. filePath: savedFilePath,
  18. showMenu: true,
  19. success: function (res) {
  20. that.setData({
  21. text: '预览成功,点击返回继续',
  22. });
  23. },
  24. fail: function (err) {
  25. wx.showToast({
  26. title: '预览失败',
  27. icon: 'error',
  28. duration: 1500
  29. })
  30. that.setData({
  31. text: '预览失败'
  32. });
  33. }
  34. });
  35. },
  36. /**
  37. * 生命周期函数--监听页面加载
  38. */
  39. onLoad(o) {
  40. if (o.url) {
  41. wx.showLoading({
  42. title: '下载中', //提示文字
  43. mask: true, //是否显示透明蒙层,防止触摸穿透,默认:false
  44. })
  45. let that = this;
  46. console.log('--------url路径', decodeURI(o.url));
  47. const url = decodeURI(o.url)
  48. // 拿到后缀
  49. const fiLeHouZuiArr =url.split('.')
  50. const fiLeHouZui =fiLeHouZuiArr[fiLeHouZuiArr.length-1]
  51. wx.downloadFile({
  52. url,
  53. success(res) {
  54. wx.getFileSystemManager().saveFile({ //微信保存文件,这个存储有点复杂
  55. // 临时存储路径,先有临时存储路径方可使用wx官方的存储本地路径( wx.env.USER_DATA_PATH )
  56. tempFilePath: res.tempFilePath,
  57. //定义本地的存储路径及名称
  58. filePath: wx.env.USER_DATA_PATH + '/' + Date.now() + '.'+fiLeHouZui,
  59. success(res) {
  60. // console.log('pppppp', res);
  61. const savedFilePath = res.savedFilePath;
  62. // console.log('xxxx', savedFilePath);
  63. wx.hideLoading()
  64. wx.showModal({
  65. title: '即将预览文件',
  66. content: '点击右上角即可保存(ios手机需要发送给文件助手后保存)',
  67. success: function (res) {
  68. if (res.confirm) {
  69. that.lookFileFu(savedFilePath,that)
  70. } else {
  71. that.lookFileFu(savedFilePath,that)
  72. }
  73. }
  74. });
  75. },
  76. fail(err) {
  77. wx.showToast({
  78. title: '预览失败',
  79. icon: 'error',
  80. duration: 1500
  81. })
  82. that.setData({
  83. text: '预览失败'
  84. });
  85. }
  86. })
  87. },
  88. fail(err) {
  89. wx.hideLoading()
  90. wx.showToast({
  91. title: '下载失败',
  92. icon: 'error',
  93. duration: 1500
  94. })
  95. console.log(err)
  96. that.setData({
  97. text: '下载失败'
  98. });
  99. }
  100. })
  101. }
  102. },
  103. /**
  104. * 生命周期函数--监听页面初次渲染完成
  105. */
  106. onReady() {
  107. },
  108. /**
  109. * 生命周期函数--监听页面显示
  110. */
  111. onShow() {
  112. },
  113. /**
  114. * 生命周期函数--监听页面隐藏
  115. */
  116. onHide() {
  117. },
  118. /**
  119. * 生命周期函数--监听页面卸载
  120. */
  121. onUnload() {
  122. },
  123. /**
  124. * 页面相关事件处理函数--监听用户下拉动作
  125. */
  126. onPullDownRefresh() {
  127. },
  128. /**
  129. * 页面上拉触底事件的处理函数
  130. */
  131. onReachBottom() {
  132. },
  133. /**
  134. * 用户点击右上角分享
  135. */
  136. onShareAppMessage() {
  137. }
  138. })