Download and save the WeChat applet file

Background: A file download function was required for a certain time. The user could not find the path after downloading the document using wx.saveFile, and then consulted the document to solve the problem by using a curve, and got wx.openDocument, but it still couldn’t be saved when it was opened, and it couldn’t be found when it was closed. It is found that it needs to be manually set to share, so that the sharing function can appear in the upper right corner. The curve solves the demand. The data and code records are as follows:

wx.downloadFile({
        url: 'https://*******',//文件路径
        success: function (res) {
          const filePath = res.tempFilePath
          wx.openDocument({
            filePath: filePath,
            showMenu: true,
            success: function (res) {
              console.log('打开文档成功')
            }
          })
        }
      })

The APIs involved in the process are documented as follows:

1.wx.saveFile(Object object)

Save the file locally. Note: saveFile will move the temporary file, so the tempFilePath passed in after the call is successful will not be available

Official document: wx.saveFile(Object object) | WeChat Open Documentation WeChat Developer Platform Documentation https://developers.weixin.qq.com/miniprogram/dev/api/file/wx.saveFile.html Problem: Find after saving successfully no local files

2. wx.openDocument(Object object)

New Page opens the document. The menu button in the upper right corner is displayed by default before the WeChat client  7.0.12 version, and it is not displayed by default in later versions, and it needs to be imported actively  showMenu.


Official document:  wx.openDocument(Object object) | WeChat Open Document WeChat Developer Platform Document https://developers.weixin.qq.com/miniprogram/dev/api/file/wx.openDocument.html

Note: filePath is a temporary path and needs to be obtained through downLoadFile. The following introduces wx.downloadFile(Object object)

3.wx.downloadFile(Object object)

Download the file resource to the local. The client directly initiates an HTTPS GET request and returns the local temporary path (local path) of the file. The maximum file size allowed for a single download is 200MB. Please read the relevant instructions before use .

Note: Please specify reasonable  Content-Type fields in the header of the response from the server to ensure that the client handles the file type correctly.


Official Documentation: DownloadTask | WeChat Open Documentation WeChat Developer Platform Documentation https://developers.weixin.qq.com/miniprogram/dev/api/network/download/wx.downloadFile.html 

 According to the above API, using the code given at the beginning of the article can realize the requirement of downloading files and finding files at any time. Flowers, it's over.

ps: Like it a lot, follow it a lot, and chat in the comment area if you have something to do.

Guess you like

Origin blog.csdn.net/wh_xmy/article/details/122601038