Skip to main content
Version: Next

Taro.downloadFile(option)

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.

Reference

Type

(option: Option) => DownloadTask

Parameters

Option

PropertyTypeRequiredDescription
urlstringYesURL to download resources
filePathstringNoIndicates the path to save downloaded files
headerRecord<string, any>NoHTTP request Header. Referer is not available in Header.
complete(res: CallbackResult) => voidNoThe callback function used when the API call completed (always executed whether the call succeeds or fails)
fail(res: CallbackResult) => voidNoThe callback function for a failed API call
success(res: CallbackResult) => voidNoThe callback function for a successful API call

FileSuccessCallbackResult

PropertyTypeDescription
filePathstringUser file path. It is returned when the filePath is specified. Same as the passed filePath.
statusCodenumberHTTP status code returned by the developer server
tempFilePathstringTemporary 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.
errMsgstringCall result

Sample Code

Taro.downloadFile({
url: 'https://example.com/audio/123', //This value for demonstration purposes only is not a real resource.
success: function (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) {
Taro.playVoice({
filePath: res.tempFilePath
})
}
}
})

API Support

APIWeChat Mini-ProgramBaidu Smart-MiniProgramAlipay Mini-ProgramH5React Native
Taro.downloadFile✔️✔️✔️✔️✔️