【微信小程序】小程序如何实现文件的预览,以PDF文件为例。

版权声明:https://blog.csdn.net/qq_37949737?assign_skin=skin-ink https://blog.csdn.net/qq_37949737/article/details/86589989

安卓系统和ios系统

    一开始误解了微信的意思了,以为安卓预览文件需要调用wx.downloadFile()和wx.openDocument()这两个API,而ios系统预览文件则是要用webview标签才能实现,最后捣鼓了好长时间ios用webview 标签预览文件的时候一直显示空白。不知道为什么(至今也没能解决),官方给出的答案是webview不能直接预览文件(也没搞明白嘛意思?)。

    所以,记住了安卓和ios预览文件的时候都可以直接调用wx.downloadFile()和wx.openDocument()这两个API就可以了,千万别再看那个什么ios预览文件需要用webview才能实现的帖子了。(全是误导你的)

 wx.downloadFile({
      url: 'http://**.*****.***/reshaiwai/demo.pdf',      //要预览的PDF的地址
      success: function (res) {                           
        console.log(res);
        if (res.statusCode === 200) {                     //成功
          var Path = res.tempFilePath                     //返回的文件临时地址,用于后面打开本地预览所用
          wx.openDocument({
            filePath: Path,                               //要打开的文件路径
            success: function (res) {
              console.log('打开PDF成功');
            }
          })
        }
      },
      fail: function (res) {
        console.log(res);                                  //失败
      }
  })

   注释:web-view下暂不支持此API 

wx.downloadFile()的属性如下:

属性 类型 默认值 必填 说明 最低版本
url string   下载资源的 url  
header Object   HTTP 请求的 Header,Header 中不能设置 Referer  
filePath string   指定文件下载后存储的路径 1.8.0
success function   接口调用成功的回调函数  
fail function   接口调用失败的回调函数  
complete function   接口调用结束的回调函数(调用成功、失败都会执行)

wx.openDocument()的属性如下:

属性 类型 默认值 必填 说明 最低版本
filePath string   文件路径,可通过 downloadFile 获得  
fileType string   文件类型,指定文件类型打开文件 1.4.0
success function   接口调用成功的回调函数  
fail function   接口调用失败的回调函数  
complete function   接口调用结束的回调函数(调用成功、失败都会执行)

wx.openDocument()打开的文件格式有:

说明
doc doc 格式
docx docx 格式
xls xls 格式
xlsx xlsx 格式
ppt ppt 格式
pptx pptx 格式
pdf pdf 格式

 

 备注:此上仅代表我自己的个人看法,有不足的地方还望告知。此后加油更正!

猜你喜欢

转载自blog.csdn.net/qq_37949737/article/details/86589989
今日推荐