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.
Type
(option: Option) => DownloadTask
Parameters
Option
Property | Type | Required | Description |
---|---|---|---|
url | string | Yes | URL to download resources |
filePath | string | No | Indicates the path to save downloaded files |
header | Record<string, any> | No | HTTP request Header. Referer is not available in Header. |
complete | (res: CallbackResult) => void | No | The callback function used when the API call completed (always executed whether the call succeeds or fails) |
fail | (res: CallbackResult) => void | No | The callback function for a failed API call |
success | (res: CallbackResult) => void | No | The callback function for a successful API call |
FileSuccessCallbackResult
Property | Type | Description |
---|---|---|
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 |
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. |
errMsg | string | Call 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
API | WeChat Mini-Program | Baidu Smart-MiniProgram | Alipay Mini-Program | H5 | React Native |
---|---|---|---|---|---|
Taro.downloadFile | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |