WeChat applet about downloading files and opening file preview files (wx.downloadFile and wx.openDocument)

Download the file and open the file

Sample API

wx.downloadFile({
    
    
  url: 'https://example.com/audio/123', //仅为示例,并非真实的资源
  success (res) {
    
    
    // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
    if (res.statusCode === 200) {
    
    
      wx.playVoice({
    
    
        filePath: res.tempFilePath
      })
    }
  }
})
wx.downloadFile({
    
    
  // 示例 url,并非真实存在
  url: 'http://example.com/somefile.pdf',
  success: function (res) {
    
    
    const filePath = res.tempFilePath
    wx.openDocument({
    
    
      filePath: filePath,
      success: function (res) {
    
    
        console.log('打开文档成功')
      }
    })
  }
})

Project example code

  wx.downloadFile({
    
    
      url: this.data.homeworkPicture,
      success: function (res) {
    
    
        var filePath = res.tempFilePath
         wx.openDocument({
    
    
           filePath: filePath,
           success: function (res) {
    
    
             console.log('打开文档成功')
           }
         })
      }
    })

Guess you like

Origin blog.csdn.net/qq_43263320/article/details/113765300