小程序预览文本文件,以及遇到的问题(出现图片格式的文件预览处理方式)wx.downloadFile wx.openDocument

// 预览文本文件
function looksDocument(url) {
  const type = url.split('.')[url.split('.').length - 1] // 截取文件后缀知晓类型.doc .txt 
  wx.downloadFile({
    url: url,      //文件路径
    header: {},
    success: function (res) {
      console.log(res)
      var filePath = res.tempFilePath;
      console.log(filePath);
      wx.openDocument({
        filePath: filePath,
        fileType: type,   //文件类型
        success: function (res) {
          console.log('打开文档成功')
        },
        fail: function (res) {
          console.log(res);
        },
        complete: function (res) {
          console.log(res);
        }
      })
    },
    fail: function (res) {
      console.log('文件下载失败');
    },
    complete: function (res) { },
  })
}

上述封装的一个方法,传入文件url即可实现预览文件内容,如果遇见文件格式出现图片格式,jpg/jpeg/png等用type判断然后去调用图片的预览方法(具体可以看我之前的博客“小程序图片点击放大效果封装,多张图片数组形式切换,wx.previewImage的使用”),

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值