WeChat Open Platform

Mini Programs Documentation

File Handling

wx.saveFile(OBJECT)

Saves files locally.

OBJECT parameter descriptions:

Parameter Type Required Description
tempFilePath String Yes Temporary path for files that need to be saved
success Function No Saved file path returned, res = {savedFilePath: 'Saved file path'}
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

success return parameter descriptions:

Parameter Description
savedFilePath Saved file path

Sample code:

wx.chooseImage({
  success: function(res) {
    var tempFilePaths = res.tempFilePaths
    wx.saveFile({
      tempFilePath: tempFilePaths[0],
      success: function(res) {
        var savedFilePath = res.savedFilePath
      }
    })
  }
})

Bugs & Tips

  1. tip: Local file storage size limit is 10MB.

wx.getSavedFileList(OBJECT)

Gets list of files saved locally.

OBJECT parameter descriptions:

Parameter Type Required Description
success Function No Callback function for successful interface call, see success return parameter descriptions for returned results
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

success return parameter descriptions:

Parameter Type Description
errMsg String Interface call result
fileList Object Array File list

fileList item descriptions:

Key Type Description
filePath String Local file path
createTime Number Timestamp for when the file was saved, the number of seconds from 08:00:00 on January 1, 1970 to the current time
size Number File size, unit B

Sample code:

wx.getSavedFileList({
  success: function(res) {
    console.log(res.fileList)
  }
})

wx.getSavedFileInfo(OBJECT)

Gets file information for local files.

OBJECT parameter descriptions:

Parameter Type Required Description
filePath String Yes File path
success Function No Callback function for successful interface call, see success return parameter descriptions for returned results
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

success return parameter descriptions:

Parameter Type Description
errMsg String Interface call result
size Number File size, unit B
createTime Number Timestamp for when the file was saved, the number of seconds from 08:00:00 on January 1, 1970 to the current time

Sample code:

wx.getSavedFileInfo({
  filePath: 'wxfile://somefile', //Only used as an example, not a real file path
  success: function(res) {
    console.log(res.size)
    console.log(res.createTime)
  }
})

wx.removeSavedFile(OBJECT)

Deletes locally stored files.

OBJECT parameter descriptions:

Parameter Type Required Description
filePath String Yes File path that needs to be deleted
success Function No Callback function for successful interface call
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

Sample code:

wx.getSavedFileList({
  success: function(res) {
    if (res.fileList.length > 0){
      wx.removeSavedFile({
        filePath: res.fileList[0].filePath,
        complete: function(res) {
          console.log(res)
        }
      })
    }
  }
})

wx.openDocument(OBJECT)

Opens a document on a new page. Supported formats: doc, xls, ppt, pdf, docx, xlsx, pptx.

OBJECT parameter descriptions:

Parameter Description Required Description
filePath String Yes File path, can be obtained through downFile
success Function No Callback function for successful interface call
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

Sample code

wx.downloadFile({
  url: 'http://example.com/somefile.pdf',
  success: function (res) {
    var filePath = res.tempFilePath
    wx.openDocument({
      filePath: filePath,
      success: function (res) {
        console.log('Document opened successfully')
      }
    })
  }
})