Network APIs

downloadFile

Download a file resource to a local location.

Overview

my.downloadFile downloads a file from a remote server to a local location. The downloaded file path is returned in the success callback and can be used with other APIs like my.previewImage or my.openDocument.

Sample Code

my.downloadFile({
  url: 'http://img.example.com/example.jpg',
  success: ({ apFilePath }) => {
    my.previewImage({
      urls: [apFilePath],
    });
  },
  fail: (res) => {
    my.alert({
      content: res.errorMessage || res.error,
    });
  },
});

Parameters

PropertyTypeRequiredDescription
urlStringYesDownloading file address.
headerObjectNoHTTP request Header.
successFunctionNoCallback function upon call success.
failFunctionNoCallback function upon call failure.
completeFunctionNoCallback function upon call completion (to be executed upon either call success or failure).

Success Callback Function

The incoming parameter is of the Object type with the following attributes:

PropertyTypeDescription
apFilePathStringTemporary file storage location.

Error Code

ErrorDescription
12Downloading failed.
13No right.

Usage Notes

  • The downloaded file is stored in a temporary location
  • Use the returned apFilePath with APIs like my.previewImage, my.openDocument, or my.saveFile
  • The file must be accessible via HTTP/HTTPS
  • Custom headers can be set via the header parameter
  • The temporary file may be cleaned up by the system, use my.saveFile to persist it