tencent cloud

Feedback

Download

Last updated: 2024-03-05 15:00:57

    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 WeChat's 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.
    timeout
    number
    No
    Timeout duration, measured in milliseconds (ms).
    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.
    profile
    Object
    Some debugging information during the network request process.
    Structure attributes of "profile":
    Structure attributes
    Type
    Description
    redirectStart
    number
    The time of the first HTTP redirect. This is only applicable for redirects within the same domain. Otherwise the value is 0.
    redirectEnd
    number
    The time of completing the last HTTP redirect. This is only applicable for internal redirects within the same domain. Otherwise the value is 0.
    fetchStart
    number
    The time at which the component is ready to fetch resources using an HTTP request, occurring prior to the inspection of the local cache.
    domainLookupStart
    number
    The time at which the DNS domain name query begins. If the local cache is used (i.e., no DNS query) or a persistent connection is established, it is equivalent to the fetchStart value.
    domainLookupEnd
    number
    The time at which the DNS domain name query completes. If the local cache is used (i.e., no DNS query) or a persistent connection is established, it is equivalent to the fetchStart value.
    connectStart
    number
    The time at which the HTTP (TCP) connection begins to establish. If a persistent connection is in place, it is equivalent to the fetchStart value.
    connectEnd
    number
    The time at which the HTTP (TCP) connection is fully established (handshake completed). If a persistent connection is in place, it is equivalent to the fetchStart value. Note that if an error occurs at the transport layer and the connection is reestablished, the time displayed here is the completion of the newly established connection.
    SSLconnectionStart
    number
    The time at which the SSL connection is established. If it is not a secure connection, the value is 0.
    SSLconnectionEnd
    number
    The time at which the SSL establishment is completed. If it is not a secure connection, the value is 0.
    requestStart
    number
    The time at which the HTTP request begins to read the actual document (completion of connection establishment), including reading from the local cache. In the event of a connection error and reconnection, the time displayed here is the time of the newly established connection.
    requestEnd
    number
    The time at which the HTTP request finishes reading the actual document.
    responseStart
    number
    The time at which the HTTP begins to receive the response (upon receiving the first byte), including reading from the local cache.
    responseEnd
    number
    The time at which the HTTP response is fully received (upon receiving the last byte), including reading from the local cache.
    rtt
    number
    The real-time round-trip time (rtt) during the connection process of the current request.
    estimate_nettype
    number
    Assessed network status: unknown, offline, slow 2G, 2G, 3G, 4G, last/0, 1, 2, 3, 4, 5, 6.
    httpRttEstimate
    number
    The protocol layer assesses the current network's round-trip time (rtt) based on multiple requests (for reference only).
    transportRttEstimate
    number
    The protocol layer assesses the current network's round-trip time (rtt) based on multiple requests (for reference only).
    downstreamThroughputKbpsEstimate
    number
    Assess the kilobits per second (kbps) of the current network download.
    throughputKbps
    number
    The actual download kilobits per second (kbps) of the current network.
    peerIP
    string
    IP of the current request.
    port
    number
    Port of the current request
    socketReused
    boolean
    Whether to reuse the connection.
    sendBytesCount
    number
    The number of bytes transmitted
    receivedBytedCount
    number
    The number of bytes received
    protocol
    string
    Protocol type in use, with valid values: http1.1, h2, quic, unknown.
    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