Wechat applet development (6) - download files from the server and open them

Keywords: Mobile Excel

The file downloaded by the wx.downloadFile interface of wechat applet is downloaded to the wechat server, and the return value is a temporary address. To save for a long time, call the wx.saveFile interface to save the file to the local cache. Although it is saved to the mobile phone, you cannot open it through the document management of the mobile phone. After you get the temporary path or cache path, you can call the wx.openDocument to open the downloaded file. There are restrictions on the file format that needs to be opened. You can only open doc, docx, xls, xlsx, ppt, pptx, pdf.

Wechat applet save file cannot specify file save path.

app.requestData({
      url: 'operationsRecord/excel/get',
      data: {'ids':that.data.selectRecordsId.toString()},
      success:function(res){
        if(res.data.success){
          let fileName = res.data.data.split('\\');
          fileName = fileName[fileName.length-1];
          console.log(fileName);
          // Download files from your own server
          const downloadTask = wx.downloadFile({
            url: app.globalData.fileUrl + 'uploads/file/'+fileName,
            success: function(res){
              console.log(res)
              // As long as the server has response data, it will write the response content to the file and enter the success callback. The business needs to determine whether it has downloaded the desired content by itself
              if (res.statusCode === 200) {
                // Transfer temporary address to local cache
                wx.saveFile({
                  tempFilePath: res.tempFilePath,
                  success: function (res) {
                    console.log(res);
                    var savedFilePath = res.savedFilePath;
                    console.log('File downloaded to' + savedFilePath);
                    // View the list of downloaded files
                    wx.getSavedFileList({
                      success: function (res){
                        console.log(res);
                      }
                    })
                    // open documents
                    wx.openDocument({
                      filePath: savedFilePath,
                      success: function (res) {
                        console.log('Document opened successfully')
                      }
                    })
                  }
                })
              }
            },
            fail: function(res){
              console.log(res)
            }
          });// end of downloadTask
          downloadTask.onProgressUpdate((res) => {
              console.log('Download progress', res.progress)
              console.log('Length of downloaded data', res.totalBytesWritten)
              console.log('Expected total length of data to be downloaded',            res.totalBytesExpectedToWrite)
          })
        }
        console.log(res);
      }// end of request.success
})

 

Posted by courtewing on Wed, 25 Dec 2019 09:43:44 -0800