小程序预览txt类型文件

发布于:2022-12-27 ⋅ 阅读:(620) ⋅ 点赞:(0)
   if(fileType==='txt'){    

            wx.navigateTo({

            url: `/packageBeforeClass/pages/txtView/txtView?fileName=${fileName}&fileType=${fileType}&url=${url}`,

          });    //如果是txt文件,跳转到txtTextView 页





// txtTextView 页

 /**

   * 生命周期函数--监听页面加载

   */

  onLoad: function (options) {

    wx.setNavigationBarTitle({

      title: `${options.fileName}.${options.fileType}`

    });

    let fs = wx.getFileSystemManager(); 

      const that=this;

      let downloadTask = wx.downloadFile({

        url: options.url,

        filePath: `${wx.env.USER_DATA_PATH}/${options.fileName}.${options.fileType}`,

        success: function () {

          wx.hideLoading(); 

          fs.readFile({

            filePath: `${wx.env.USER_DATA_PATH}/${options.fileName}.${options.fileType}`,

            encoding:'utf8',

            complete(res){

              that.setData({

                txtTextView:res.data     //在wxml做展示

              });

            }

          })  

        },

        fail: function(error) {  

          wx.hideLoading();

          wx.showToast({

            title: error.errMsg,

            icon: "none"

          });

        }

      });

  },


网站公告

今日签到

点亮在社区的每一天
去签到