File

更新时间:
复制 MD 格式

Use these APIs to save, retrieve, list, and delete local files in mPaaS mini programs, and to preview PDF documents.

my.saveFile

Note

Supported in base library 1.13.0+ and mPaaS 10.1.32+. For earlier versions, ensure compatibility. For more information, see Mini Program base libraries.

Saves a file to the local device (total local file size limit: 10 MB). After calling my.saveFile, saved files are in the /alipay/pictures/ folder on Android. On iOS, the folder path is hidden.

Input parameters

Name

Type

Required

Description

apFilePath

String

Yes

File path

success

Function

No

Called on success.

fail

Function

No

Called on failure.

complete

Function

No

Called when the call completes, regardless of outcome.

Success callback result

Name

Type

Description

apFilePath

String

Path of the saved file.

Code sample

my.chooseImage({
  success: (res) => {
    my.saveFile({
      apFilePath: res.apFilePaths[0],
      success: (res) => {
        console.log(JSON.stringify(res))
      },
    });
  },
});

my.getFileInfo

Note

Supported in base library 1.4.0+ and mPaaS 10.1.32+. For earlier versions, ensure compatibility. For more information, see Mini Program base libraries.

Input parameters

Name

Type

Required

Description

apFilePath

String

Yes

File path (local path).

digestAlgorithm

String

No

Digest algorithm. Valid values: md5 and sha1. Default: md5.

success

Function

No

Called on success.

fail

Function

No

Called on failure.

complete

Function

No

Called when the call completes, regardless of outcome.

Success callback result

Name

Type

Description

size

Number

File size.

digest

String

Digest result.

Code sample

my.getFileInfo({
    apFilePath:'https://resource/apml953bb093ebd2834530196f50a4413a87.video',
    digestAlgorithm:'sha1',
    success:(res)=>{
        console.log(JSON.stringify(res))
    }
})

my.getSavedFileInfo

Note

Supported in base library 1.3.0+ and mPaaS 10.1.32+. For earlier versions, ensure compatibility. For more information, see Mini Program base libraries.

Retrieves information about a saved file.

Input parameters

Name

Type

Required

Description

apFilePath

String

Yes

File path.

success

Function

No

Called on success.

fail

Function

No

Called on failure.

complete

Function

No

Called when the call completes, regardless of outcome.

Success callback result

Name

Type

Description

size

Number

File size.

createTime

Number

Creation timestamp.

Code sample

my.getSavedFileInfo only works with paths saved by my.saveFile.

var that = this;
    my.chooseImage({
    success: (res) => {
      console.log(res.apFilePaths[0], 1212)
      my.saveFile({
        apFilePath: res.apFilePaths[0],
        success: (result) => {
          console.log(result, 1212)
          my.getSavedFileInfo({
            apFilePath: result.apFilePath,
            success: (resu) => {
              console.log(JSON.stringify(resu))
              that.filePath = resu
            }
          })
        },
      });
    },
});

my.getSavedFileList

Note

Supported in base library 1.13.0+ and mPaaS 10.1.32+. For earlier versions, ensure compatibility. For more information, see Mini Program base libraries.

Retrieves all saved files.

Input parameters

Name

Type

Required

Description

success

Function

No

Called on success.

fail

Function

No

Called on failure.

complete

Function

No

Called when the call completes, regardless of outcome.

Success callback result

Name

Type

Description

fileList

List

File list.

File object properties

Name

Type

Description

size

Number

File size.

createTime

Number

Creation time.

apFilePath

String

File path.

Code sample

my.getSavedFileList({
    success:(res)=>{
        console.log(JSON.stringify(res))
    }
});

my.removeSavedFile

Note

Supported in base library 1.13.0+ and mPaaS 10.1.32+. For earlier versions, ensure compatibility. For more information, see Mini Program base libraries.

Deletes a saved file.

Input parameters

Name

Type

Required

Description

apFilePath

String

Yes

File path.

success

Function

No

Called on success.

fail

Function

No

Called on failure.

complete

Function

No

Called when the call completes, regardless of outcome.

Code sample

my.getSavedFileList({
        success:(res)=>{
            my.removeSavedFile({
          apFilePath:res.fileList[0].apFilePath,
          success:(res)=>{
            console.log('remove success')
          }
        })
        }
    });

my.openDocument

my.openDocument(Object)

Previews local PDF files.

Input parameters

Object

Property

Type

Default

Required

Compatibility

Description

filePath

String

-

Yes

-

File path (temporary file, cache file, or user file).

fileType

String

-

Yes

-

File type.

Valid value: pdf.

success

Function

-

No

-

Called on success.

fail

Function

-

No

-

Called on failure.

complete

Function

-

No

-

Called when the call completes.

Note

Runs regardless of outcome.

Error codes

The fail callback returns an Object with error (error code) and errorMessage (error description) properties.

Error code

Error message

Solutions

4011

Invalid path

Check if the filePath parameter is correct. For more information, see the type and description of the filePath parameter.

4012

The file does not exist

Check if the file specified by `filePath` exists.

4013

This file type is not supported

Check if the `fileType` parameter is correct. For more information, see the description of the fileType parameter.

Code sample

my.openDocument({
  filePath: `${my.env.USER_DATA_PATH}/test.pdf`,
  fileType: 'pdf',
  success: (res) => {
    console.log(res);
  },
  fail: (err) => {
    console.log(err);
  },
});

Tip

Download a PDF with my.downloadFile, then pass the temporary file path as the filePath parameter.