tt.openDocument
收藏
我的收藏

基础库 1.85.0 开始支持本方法,这是一个异步方法。

新开页面打开文档,支持格式:doc,docx,xls,xlsx,ppt,pptx,pdf。

前提条件
业务背景
使用限制
  • Tip: iOS 暂不支持预览 ppt;
  • Tip: Android 暂不支持小程序内预览
注意事项
支持沙盒
相关教程

语法

tt.openDocument(options)

参数说明

options 为 object 类型,属性如下:

属性名类型默认值必填说明最低支持版本
fileTypeenum
文件类型,指定文件类型打开文件,有效值 doc,docx,xls,xlsx,ppt,pptx,pdf
1.85.0
fileNamestring
文件名称,用于保存到手机以及预览页面标题
1.85.0
filePathstring
文件路径,可通过 downloadFile 获得
1.85.0
successfunction
接口调用成功的回调函数
1.85.0
failfunction
接口调用失败的回调函数
1.85.0
completefunction
接口调用结束的回调函数(调用成功、失败都会执行)
1.85.0

fileType 的合法值

说明最低支持版本
doc
文件类型,指定文件类型打开文件
1.85.0
docx
文件类型,指定文件类型打开文件
1.85.0
xls
文件类型,指定文件类型打开文件
1.85.0
xlsx
文件类型,指定文件类型打开文件
1.85.0
ppt
文件类型,指定文件类型打开文件
1.85.0
pptx
文件类型,指定文件类型打开文件
1.85.0
pdf
文件类型,指定文件类型打开文件
1.85.0

回调成功

object 类型,属性如下:

属性名类型说明最低支持版本
errMsgstring
"openDocument:ok"
1.85.0

回调失败

object 类型,属性如下:

属性名类型说明最低支持版本
errMsgstring
"openDocument:fail" + 错误信息
1.85.0

错误码

errNoerrMsg说明最低支持版本
20000openDocument:fail filePath is required
filePath 参数错误
1.85.0
21101openDocument:fail no such file or directory %s
找不到此文件
1.85.0
21102openDocument:fail permission denied, read %s
无文件访问权限
1.85.0
21103openDocument:fail Can't support this format.
不支持的文件格式
1.85.0
21104"openDocument:fail operation not permitted %s"
filePath 类型不是文件
2.60.0

扫码体验

请使用字节宿主APP扫码

代码示例

开发者工具中预览

tt.downloadFile({
  // 仅为示例 url,并非真实地址
  url: "http://example.com/somefile.pdf",
  success: function (res) {
    const filePath = res.tempFilePath
    tt.openDocument({
      filePath: filePath,
      success: function (res) {
        console.log("打开文档成功")
      },
    })
  },
})