文件

my.saveFile

说明

基础库 1.13.0 及以上版本支持该接口,低版本需做兼容处理,操作参见 小程序基础库说明。mPaaS 10.1.32 及以上版本支持该接口。

该接口用于保存文件到本地(本地文件大小总容量限制:10M)。调用 my.saveFile 成功后,安卓系统可在手机存储 /alipay/pictures/ 文件位置查看保存的文件;iOS 系统无法查看被隐藏的目录路径。

入参

名称

类型

必填

描述

apFilePath

String

文件路径

success

Function

调用成功的回调函数

fail

Function

调用失败的回调函数

complete

Function

调用结束的回调函数(调用成功、失败都会执行)

success 返回值说明

名称

类型

描述

apFilePath

String

文件保存路径

代码示例

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

my.getFileInfo

说明

基础库 1.4.0 及以上版本支持该接口,低版本需做兼容处理,操作参见 小程序基础库说明。mPaaS 10.1.32 及以上版本支持该接口。

入参说明

名称

类型

必填

描述

apFilePath

String

文件路径(本地路径)

digestAlgorithm

String

摘要算法,支持 md5sha1 ,默认为 md5

success

Function

调用成功的回调函数

fail

Function

调用失败的回调函数

complete

Function

调用结束的回调函数(调用成功、失败都会执行)

success 返回值说明

名称

类型

描述

size

Number

文件大小

digest

String

摘要结果

代码示例

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

my.getSavedFileInfo

说明

基础库 1.3.0 及以上版本支持该接口,低版本需做兼容处理,操作参见 小程序基础库说明。mPaaS 10.1.32 及以上版本支持该接口。

该接口用于获取保存的文件信息。

入参

名称

类型

必填

描述

apFilePath

String

文件路径

success

Function

调用成功的回调函数

fail

Function

调用失败的回调函数

complete

Function

调用结束的回调函数(调用成功、失败都会执行)

success 返回值说明

名称

类型

描述

size

Number

文件大小

createTime

Number

创建时间的时间戳

代码示例

使用 my.saveFile 保存的地址才能够使用 my.getSavedFileInfo

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

说明

基础库 1.13.0 及以上版本支持该接口,低版本需做兼容处理,操作参见 小程序基础库说明。mPaaS 10.1.32 及以上版本支持该接口。

该接口用于获取保存的所有文件。

入参

名称

类型

必填

描述

success

Function

调用成功的回调函数

fail

Function

调用失败的回调函数

complete

Function

调用结束的回调函数(调用成功、失败都会执行)

success 返回值说明

名称

类型

描述

fileList

List

文件列表

File 对象属性说明

名称

类型

描述

size

Number

文件大小

createTime

Number

创建时间

apFilePath

String

文件路径

代码示例

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

my.removeSavedFile

说明

基础库 1.13.0 及以上版本支持该接口,低版本需做兼容处理,操作参见 小程序基础库说明。mPaaS 10.1.32 及以上版本支持该接口。

该接口用于删除某个保存的文件。

入参

名称

类型

必填

描述

apFilePath

String

文件路径

success

Function

调用成功的回调函数

fail

Function

调用失败的回调函数

complete

Function

调用结束的回调函数(调用成功、失败都会执行)

代码示例

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

my.openDocument

my.openDocument(Object object)

预览本地 PDF 文件。

入参

Object object

属性

类型

默认值

必填

兼容性

描述

filePath

String

-

-

文件路径(本地临时文件、本地缓存文件、本地用户文件)。

fileType

String

-

-

文件类型。

可选值:pdf。

success

Function

-

-

调用成功的回调函数。

fail

Function

-

-

调用失败的回调函数。

complete

Function

-

-

调用结束的回调函数。

说明

调用成功、失败都会执行。

错误码

fail 回调的参数为 Object,error 属性为错误码,errorMessage 属性为错误消息。

错误码

错误消息

解决方案

4011

路径不合法

检查传入的文件路径 filePath 是否正确,可参考入参 filePath 的类型和描述。

4012

文件不存在

请检查传入 filePath 对应的文件是否存在。

4013

暂不支持此文件类型

请检查传入的 fileType 是否正确,可参考入参 fileType 的描述。

代码示例

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

提示

通过 my.downloadFile 下载 PDF ,获取本地临时文件路径作为 filePath 参数。