title: Taro.downloadFile(option)

sidebar_label: downloadFile

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

  1. (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

  1. Taro.downloadFile({
  2. url: 'https://example.com/audio/123', //This value for demonstration purposes only is not a real resource.
  3. success: function (res) {
  4. // 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.
  5. if (res.statusCode === 200) {
  6. Taro.playVoice({
  7. filePath: res.tempFilePath
  8. })
  9. }
  10. }
  11. })

API Support

API WeChat Mini-Program Baidu Smart-MiniProgram Alipay Mini-Program H5 React Native
Taro.downloadFile ✔️ ✔️ ✔️ ✔️ ✔️