WeChat Open Platform

Mini Programs Documentation

Uploads/Downloads

wx.uploadFile(OBJECT)

Uploads local resources to the developer's server. After a page gets a local resource temporary file path through the wx.chooseImage interface, it can upload the local resource to the designated server using this interface. The client initiates an HTTPS POST request, wherein content-type is multipart/form-data.

OBJECT parameter descriptions:

Parameter Type Required Description
url String Yes Developer server url
filePath String Yes Path for the file resource that needs to be uploaded
name String Yes Key corresponding to file, developers can get the file's binary content on the server side using this key
header Object No HTTP request header, referers cannot be set in the header
formData Object No Other additional form data in the HTTP request
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)

success return parameter descriptions:

Parameter Type Description
data String Data returned by developer server
statusCode Number HTTP status code

Sample code:

wx.chooseImage({
  success: function(res) {
    var tempFilePaths = res.tempFilePaths
    wx.uploadFile({
      url: 'http://example.weixin.qq.com/upload', //example only, not a real interface address
      filePath: tempFilePaths[0],
      name: 'file',
      formData:{
        'user': 'test'
      },
      success: function(res){
        var data = res.data
        //do something
      }
    })
  }
})

Bugs & Tips

  1. tip: Maximum concurrency limit is 10
  2. tip: Default timeout length and maximum timeout length are both 60s

wx.downloadFile(OBJECT)

Downloads file resources locally. The client directly initiates an HTTP GET request, a temporary local file path is returned.

OBJECT parameter descriptions:

Parameter Type Required Required
url String Yes Downloads resource url
header Object No HTTP request header
success Function No After successful download, passed to the page in the form of a tempFilePath, res = {tempFilePath: 'Temporary 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)

Note: Temporary file paths can be used as normal during this Mini Program startup. wx.saveFile needs to be called actively if files need to be saved permanently. They can only be accessed the next time the Mini Program is launched.

Sample code:

wx.downloadFile({
  url: 'http://example.com/audio/123', //example only, not a real resource
  success: function(res) {
    wx.playVoice({
      filePath: res.tempFilePath
    })
  }
})

Bugs & Tips

  1. tip: Maximum concurrency limit is 10.
  2. tip: Default timeout length and maximum timeout length are both 60s.
  3. tip: The network referer cannot be set, its format is fixed as https://servicewechat.com/{appid}/{version}/page-frame.html, wherein {appid} is the Mini Program's appid and {version} is the Mini Program's version number. If the version number is 0 this indicates that it is the development version.
  4. tip: header setting is ineffective in iOS WeChat client version 6.5.3 or earlier.