文件

优质
小牛编辑
112浏览
2023-12-01

jd.getFileInfo(Object object)

获取文件信息

参数

Object object
属性类型默认值必填说明
filePathstring本地文件路径 (本地路径)
digestAlgorithmstring'md5'计算文件摘要的算法
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.digestAlgorithm 的合法值

说明
md5md5 算法
sha1sha1 算法

object.success 回调函数

参数
Object res
属性类型说明
sizenumber文件大小,以字节为单位
digeststring按照传入的 digestAlgorithm 计算得出的的文件摘要

示例代码

jd.getFileInfo({
  success: function(res) {
    console.log(res.size)
    console.log(res.digest)
  }
})

FileSystemManager jd.getFileSystemManager()

获取全局唯一的文件管理器

返回值

FileSystemManager

文件管理器

jd.getSavedFileInfo(Object object)

获取本地文件的文件信息。此接口只能用于获取已保存到本地的文件,若需要获取临时文件信息,请使用 jd.getFileInfo() 接口。

参数

Object object
属性类型默认值必填说明
filePathstring文件路径 (本地路径)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.success 回调函数

参数
Object res
属性类型说明
sizenumber文件大小,单位 B
createTimenumber文件保存时的时间戳,从1970/01/01 08:00:00 到该时刻的秒数

示例代码

jd.getSavedFileInfo({
  success: function(res) {
    console.log(res)
  }
})

jd.getSavedFileList(Object object)

获取该小程序下已保存的本地缓存文件列表

参数

Object object
属性类型默认值必填说明
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.success 回调函数

参数
Object res
属性类型说明
fileListArray.<Object>文件数组,每一项是一个 FileItem

res.fileList 的结构

属性类型说明
filePathstring文件路径 (本地路径)
sizenumber本地文件大小,以字节为单位
createTimenumber文件保存时的时间戳,从1970/01/01 08:00:00 到当前时间的秒数

示例代码

jd.getSavedFileList({
  success: function(res) {
    console.log(res.fileList)
  }
})

jd.openDocument(Object object)

新开页面打开文档。

参数

Object object
属性类型默认值必填说明
filePathstring文件路径 (本地路径) ,可通过 downloadFile 获得
fileTypestring文件类型,指定文件类型打开文件
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.fileType 的合法值

说明
docdoc 格式
docxdocx 格式
xlsxls 格式
xlsxxlsx 格式
pptppt 格式
pptxpptx 格式
pdfpdf 格式

示例代码

jd.downloadFile({
  url: downLoadUrl,
  success: function (res) {
    const filePath = res.tempFilePath
    jd.openDocument({
      filePath: filePath,
      success: function (res) {
        console.log('打开文档成功')
      }
    })
  }
})

jd.removeSavedFile(Object object)

删除本地缓存文件

参数

Object object
属性类型默认值必填说明
failfunction接口调用失败的回调函数
successfunction接口调用成功的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
filePathstring需要删除的文件路径 (本地路径)

示例代码

jd.getSavedFileList({
 success: function(res) {
   if (res.fileList.length > 0){
     jd.removeSavedFile({
       filePath: res.fileList[0].filePath,
       complete: function(res) {
         console.log(res)
       }
     })
   }
 }
})

jd.saveFile(Object object)

保存文件到本地。注意:saveFile 会把临时文件移动,因此调用成功后传入的 tempFilePath 将不可用

参数

Object object
属性类型默认值必填说明
tempFilePathstring需要保存的文件的临时路径 (本地路径)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.success 回调函数

参数
Object res
属性类型说明
savedFilePathstring存储后的文件路径 (本地路径)

示例代码

jd.downloadFile({
    url: downLoadUrl,
    success: function(res) {
        const tempFilePath = res.tempFilePath
        jd.saveFile({
            tempFilePath: tempFilePath,
            success: function(res) {
                const savedFilePath = res.savedFilePath
            }
        })
    }
})