# DownloadTask wx.downloadFile(Object object)

Downloads local resources to the local device. The client initiates an HTTPS GET request. The local temporary path to the file is returned. The maximum file size for a single download is 50 MB. Read related instructions before use.

Note: Specify a reasonable Content-Type field in the server response header to ensure that the client handles the file type properly.

# Parameters

# Object object

Property Type Default Value Required Description Minimum Version
url string Yes URL to download resources
header Object No HTTP request Header. Referer is not available in Header.
filePath string No Indicates the path to save downloaded files 1.8.0
success function No The callback function for a successful API call
fail function No The callback function for a failed API call
complete function No The callback function used when the API call completed (always executed whether the call succeeds or fails)

# object.success callback function

# Parameters
# Object res
Property Type Description
tempFilePath string Temporary file path. It is returned when the filePath to save files is not specified. The downloaded files will be stored in a temporary file path.
filePath string User file path. It is returned when the filePath is specified. Same as the passed filePath.
statusCode number HTTP status code returned by the developer server

# Return Values

# DownloadTask

Start from base library version 1.4.0. Please remaining backward compatible.

An object that can be used to listen on the download progress change event and cancel download tasks

# Sample Code

wx.downloadFile({
  url: 'https://example.com/audio/123', //This value for demonstration purposes only is not a real resource.
  success (res) {
    // As long as there is response data in the server, the response content will be written to the file and the success callback is triggered. Judge whether desired data is downloaded depending on the service.
    if (res.statusCode === 200) {
      wx.playVoice({
        filePath: res.tempFilePath
      })
    }
  }
})
点击咨询小助手