tencent cloud

Feedback

Download

Last updated: 2024-11-21 18:41:53

    downloadFile

    This API is used via DownloadTask wx.downloadFile (Object object).
    Note:
    Please specify a reasonable Content-Type field in the server response header to ensure the client correctly processes the file type.
    Feature Description: Downloads file resources to local storage. The client directly initiates an HTTPS GET request, returning the local temporary path of the file (local path). The maximum file allowed for a single download is 200 MB. See relevant instructions before use.
    Parameter and Description: Object.
    Attribute
    Type
    Required
    Description
    url
    string
    Yes
    URL for resource download
    header
    Object
    No
    HTTP request's Header; setting Referer within the Header is not allowed.
    filePath
    string
    No
    Designated path for storing the downloaded files
    success
    function
    No
    Callback Function of Successful Interface Call
    fail
    function
    No
    Callback Function of Failing Interface Call
    complete
    function
    No
    Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)
    Parameters for object.success callback function: Object res.
    Attribute
    Type
    Description
    tempFilePath
    string
    Temporary file path (local path). If no filePath is specified for file storage, this will be returned. The downloaded files will be stored in a temporary folder.
    filePath
    string
    User file path (local path). This will be returned when a filePath is passed in, and it will be consistent with the provided filePath.
    statusCode
    number
    HTTP status code returned from the developer's server.
    Return Value:DownloadTask
    Sample Code
    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

    This method is used via DownloadTask.abort().
    Function Description: Aborts the download task.

    .onProgressUpdate

    This method is used via DownloadTask.onProgressUpdate(function listener).
    Function Description: Monitors the event of download progress changes.
    Parameter and Description: function listener, the listener function for download progress change events.
    Attribute
    Type
    Description
    progress
    number
    Download progress percentage
    totalBytesWritten
    number
    Length of the data already downloaded, measured in Bytes.
    totalBytesExpectedToWrite
    number
    Total length of the data expected to be downloaded, measured in bytes.

    .offProgressUpdate

    This method is used via DownloadTask.offProgressUpdate(function listener).
    Feature Description: Removes the listener function for download progress change events.
    Parameters and Description: function listener, the listener function passed in by onProgressUpdate. If this parameter is not passed in, all listener functions will be removed.
    Sample Code
    const listener = function (res) { console.log(res) }
    
    DownloadTask.onHeadersReceived(listener)
    DownloadTask.offHeadersReceived(listener) // The same function object as the listener must be passed in.

    .onHeadersReceived

    This method is used via DownloadTask.onHeadersReceived(function listener).
    Feature Description: Monitors HTTP Response Header events. This occurs earlier than the request completion event.
    Parameter and Description: function listener, the listener function for HTTP Response Header events.
    Attribute
    Type
    Description
    header
    Object
    HTTP Response Header returned from the developer's server.

    .offHeadersReceived

    This method is used via DownloadTask.offHeadersReceived(function listener).
    Feature Description: Removes the listener function for HTTP Response Header events.
    Parameters and Description: function listener, the listener function passed in by onHeadersReceived. If this parameter is not passed in, all listener functions will be removed.
    Sample Code
    const listener = function (res) { console.log(res) }
    
    DownloadTask.onHeadersReceived(listener)
    DownloadTask.offHeadersReceived(listener) // The same function object as the listener must be passed in.

    Sample code

    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
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support