微信小程序下载文件ios无法预览问题--解决方案

微信小程序下载文件ios无法预览问题–解决方案

想给单位做一个微信小程序,实现文件上传下载的功能,方便办事,也能提高办事效率。
wx.downloadFile()获取到的文件缓存在手机上,iOS无法在手机预览这个缓存文件,安卓手机可以。
这个需要在调用downloadFile指定保存文件路径,才可以预览操作文件,在临时路径是无法转发编辑的。

var fileTypes = ["DOC", "XLS", "PPT", "PDF", "DOCX", "XLSX", "PPTX"];
var imageTypes = ["JPG", "JPEG", "PNG"];
 var FilePath = wx.env.USER_DATA_PATH + "/"+ fileId + "."+fileType;
 wx.downloadFile({
      url: app.globalData.apiUrl + "certapps/attachment?id=" + fileId,
      filePath: FilePath,
      method: "GET",
      complete: function(res) {
        wx.hideLoading();
      },
      header: {
        'Content-Type': 'application/json',
        'token': token
      },
      success: function(res) {
        wx.hideLoading();
        if (res.statusCode === 200) {
          if (fileTypes.indexOf(fileType) != -1) {
            wx.openDocument({
              filePath: res.filePath,
              success: function(res) {
                console.log('打开文档成功')
              },
              fail: function(res) {
                console.log(res)
                wx.hideLoading();
                wx.showModal({
                  title: '提示',
                  content: "下载失败!",
                  showCancel: false
                })
              }
            })
          } else if (imageTypes.indexOf(fileType) != -1) {
            wx.previewImage({
              current: res.filePath, // 当前显示图片的http链接
              urls: [res.filePath] // 需要预览的图片http链接列表
            })
          } else {
            wx.showModal({
              title: '提示',
              content: "文件类型不支持预览",
              showCancel: false
            })
          }
        } else if (res.statusCode !== 200) {
          wx.showModal({
            title: '提示',
            content: "下载失败!",
            showCancel: false
          })
        }
      },
      fail: function(res) {
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: "下载失败!",
          showCancel: false
        })
      }
    })
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值