// pages/download/index.js Page({ /** * 页面的初始数据 */ data: { text: '下载文件' }, backUrl() { wx.navigateBack({ delta: 1 }) }, // 预览文件 lookFileFu(savedFilePath, that) { wx.openDocument({ //微信打开文件 filePath: savedFilePath, showMenu: true, success: function (res) { that.setData({ text: '预览成功,点击返回继续', }); }, fail: function (err) { wx.showToast({ title: '预览失败', icon: 'error', duration: 1500 }) that.setData({ text: '预览失败' }); } }); }, /** * 生命周期函数--监听页面加载 */ onLoad(o) { if (o.url) { wx.showLoading({ title: '下载中', //提示文字 mask: true, //是否显示透明蒙层,防止触摸穿透,默认:false }) let that = this; console.log('--------url路径', decodeURI(o.url)); const url = decodeURI(o.url) // 拿到后缀 const fiLeHouZuiArr =url.split('.') const fiLeHouZui =fiLeHouZuiArr[fiLeHouZuiArr.length-1] wx.downloadFile({ url, success(res) { wx.getFileSystemManager().saveFile({ //微信保存文件,这个存储有点复杂 // 临时存储路径,先有临时存储路径方可使用wx官方的存储本地路径( wx.env.USER_DATA_PATH ) tempFilePath: res.tempFilePath, //定义本地的存储路径及名称 filePath: wx.env.USER_DATA_PATH + '/' + Date.now() + '.'+fiLeHouZui, success(res) { // console.log('pppppp', res); const savedFilePath = res.savedFilePath; // console.log('xxxx', savedFilePath); wx.hideLoading() wx.showModal({ title: '即将预览文件', content: '点击右上角即可保存(ios手机需要发送给文件助手后保存)', success: function (res) { if (res.confirm) { that.lookFileFu(savedFilePath,that) } else { that.lookFileFu(savedFilePath,that) } } }); }, fail(err) { wx.showToast({ title: '预览失败', icon: 'error', duration: 1500 }) that.setData({ text: '预览失败' }); } }) }, fail(err) { wx.hideLoading() wx.showToast({ title: '下载失败', icon: 'error', duration: 1500 }) console.log(err) that.setData({ text: '下载失败' }); } }) } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })