Skip to content

Download

downloadFile

TIP

The API usage is as follows: DownloadTask wx.downloadFile(Object object)

TIP

Please specify a reasonable Content-Type field in the header of the server response to ensure that the client handles the file type correctly.

  • Functional description: Download file resources 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 allowed for a single download is 200MB. Please refer to the following before use Related instructions.

  • Parameters and descriptions: Object object。

    PropertiesTypeRequiredDescription
    urlstringYesThe url of the downloaded resource
    headerObjectNoHeader of the HTTP request. Referer cannot be set in the header
    filePathstringNoSpecify the path where the file is stored after downloading
    successFunctionNoCallback function for successful interface call
    failFunctionNoCallback function for failed interface call
    completeFunctionNoCallback function for interface call completion (executed regardless of success or failure)
  • object.success callback function parameters: Object res。

    PropertiesTypeDescription
    tempFilePathstringTemporary file path (local path). If the filePath is not passed in to specify the file storage path, it will return, and the downloaded file will be stored in a temporary file
    filePathstringUser file path (local path). If filePath is passed in, it will return, which is consistent with the passed in filePath
    statusCodenumberHTTP status code returned by the developer server
  • Return value: DownloadTask

  • Sample code:

js
wx.downloadFile({
  url: 'https://example.com/audio/123', // This is merely an example, not an actual resource.
  success (res) {
    // As long as the server has response data, the response content will be written into the file and enter the success callback. It is up to the business side to determine whether the desired content has been downloaded.
    if (res.statusCode === 200) {
      wx.playVoice({
        filePath: res.tempFilePath
      })
    }
  }
})

DownloadTask

.abort

TIP

The API usage is as follows: DownloadTask.abort()

  • Functional description: Interrupt the download task.

.onProgressUpdate

TIP

The API usage is as follows: DownloadTask.onProgressUpdate(function listener)

  • Functional description: Listen for download progress change events.

  • Parameters and descriptions: function listener,Listener function for download progress change events.

    PropertiesTypeDescription
    progressnumberDownload progress percentage
    totalBytesWrittennumberThe length of the data that has been downloaded, in Bytes
    totalBytesExpectedToWritenumberThe total length of the data expected to be downloaded, in Bytes

.offProgressUpdate

TIP

The API usage is as follows: DownloadTask.offProgressUpdate(function listener)

  • Functional description: Remove the listener function for download progress change events

  • Parameters and descriptions: function listener,onProgressUpdate The listener function passed in. If this parameter is not passed, all listeners will be removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

DownloadTask.onHeadersReceived(listener)
DownloadTask.offHeadersReceived(listener) // Pass the same function object used for listening.

.onHeadersReceived

TIP

The API usage is as follows: DownloadTask.onHeadersReceived(function listener)

  • Functional description: Listen to the HTTP Response Header event. It will be earlier than the request completion event.

  • Parameters and descriptions: function listener,Listener function of the HTTP Response Header event.

    PropertiesTypeDescription
    headerObjectHTTP Response Header returned by the developer server

.offHeadersReceived

TIP

The API usage is as follows: DownloadTask.offHeadersReceived(function listener)

  • Functional description: Remove the listener function of the HTTP Response Header event.

  • Parameters and descriptions: function listener,onHeadersReceived The listener function passed in. If this parameter is not passed, all listeners will be removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

DownloadTask.onHeadersReceived(listener)
DownloadTask.offHeadersReceived(listener)  // Pass the same function object used for listening.
  • Sample code:
js
const downloadTask = wx.downloadFile({
  url: 'http://example.com/audio/123', // This is merely an example, not an actual resource.
  success (res) {
    wx.playVoice({
      filePath: res.tempFilePath
    })
  }
})

downloadTask.onProgressUpdate((res) => {
  console.log('Download progress', res.progress)
  console.log('Length of data already downloaded', res.totalBytesWritten)
    console.log(''Length of data expected to be downloaded', res.totalBytesExpectedToWrite)
})

downloadTask.abort() // Aborts the download task