File APIs

chooseFileFromDisk

Choose a file to upload from the device storage.

Overview

my.chooseFileFromDisk allows users to select a file from their device storage. You can view the details of the file or delete the file you have added.

Supported types of mini programs: DSL, H5+

Sample Code

DSL

my.chooseFileFromDisk({
  success: (res) => {
    console.log(res);
  },
  fail: (err) => {
    console.log(err);
  },
});

H5+

AlipayJSBridge.call("chooseFileFromDisk", {}, function (res) {
  alert("AlipayJSBridge chooseFileFromDisk result:\n" + JSON.stringify(res));
});

Parameters

Object type with the following attributes:

PropertyTypeRequiredDescription
successFunctionNoThe callback function for a successful API call.
failFunctionNoThe callback function for a failed API call.
completeFunctionNoThe callback function used when the API call is completed. This function is always executed no matter the call succeeds or fails.

Success Callback Function

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

PropertyTypeDescription
apFilePathStringThe temporary local file path.

Failure Callback Function

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

PropertyTypeDescription
errorNumberError code.
errorMessageStringError message.

Error Codes

Error codeError messageRemark
4The JSAPI call is denied.The mini program that is developed by the merchant/ISV has no right to call the JSAPI.
12Errors occur when copying the file.
15The user cancels the file selection.
16The user denies the permission to read the storage.Android Only.
17No permission to read the storage.Android Only.
18Choosing large file is not supported.

Note: The following list describes the difference between the error code 16 and 17:

  • 16: The user denies the permission to read the storage for the current request.

File Size Limit

If you choose a file that is too large, the mini program or the wallet app cannot run properly. For this reason, the file size is limited to 50 MB. When the file size exceeds 50 MB, the error code of 18 is returned, and the corresponding error message is displayed.

Usage Notes

  • This API opens a file picker dialog allowing users to select files from their device storage
  • The selected file path is returned as a temporary local path (apFilePath)
  • The file size limit is 50 MB - larger files will return error code 18
  • On Android, storage permissions may be required (error codes 16 and 17)
  • Use the returned apFilePath with other file APIs like my.uploadFile or my.openDocument