# uni.openDocument(options?)

打开文档

# openDocument 兼容性

Web 微信小程序 Android iOS HarmonyOS
x 4.41 - - 4.61

# 参数

名称 类型 必填 默认值 兼容性 描述
options OpenDocumentOptions - - uni.openDocument参数定义
名称 类型 必备 默认值 兼容性 描述
filePath string -
文件路径,仅支持本地路径
fileType string -
文件类型,指定文件类型打开文件,有效值 doc, xls, ppt, pdf, docx, xlsx, pptx
合法值 兼容性 描述
doc - -
xls - -
ppt - -
pdf - -
docx - -
xlsx - -
pptx - -
success (res: OpenDocumentSuccess) => void -
uni.openDocument成功回调函数定义
fail (res: OpenDocumentFail) => void -
uni.openDocument失败回调函数定义
complete (res: any) => void -
uni.openDocument完成回调函数定义
showMenu boolean -
需要基础库: 2.11.0

是否显示右上角菜单

# 示例

hello uni-app x

该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验

<template>
  <view class="uni-container">
    <page-head :title="title"></page-head>
    <view class="uni-common-mt">
      <button v-for="(item, index) in fileList" :key="index" @click="openDocument(item)" :disabled="loading" class="uni-common-mt">
        打开 {{item.type}} 文件
      </button>
    </view>
  </view>
</template>

<script setup lang="uts">
type FileItem = {
  type: string,
  url: string
}

const title = 'openDocument'
const loading = ref(false)
const fileList = ref<Array<FileItem>>([
  {
    type: 'pdf',
    url: 'https://web-assets.dcloud.net.cn/unidoc/zh/helloworld.pdf'
  },
  {
    type: 'doc',
    url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.doc'
  },
  {
    type: 'docx',
    url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.docx'
  },
  {
    type: 'ppt',
    url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.ppt'
  },
  {
    type: 'pptx',
    url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.pptx'
  },
  {
    type: 'xls',
    url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.xls'
  },
  {
    type: 'xlsx',
    url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.xlsx'
  }
])

const openDocument = (item: FileItem) => {
  loading.value = true
  uni.downloadFile({
    url: item.url,
    success: (res) => {
      uni.openDocument({
        filePath: res.tempFilePath,
        success: () => {
          console.log('打开文档成功')
        },
        fail: (err) => {
          console.log('打开文档失败',err)
        }
      })
    },
    fail: (err) => {
      console.log('下载失败',err)
    },
    complete: () => {
      loading.value = false
    }
  })
}
</script>

# 参见

# 示例

hello uni-app x

该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验

<template>
  <view class="uni-container">
    <page-head :title="title"></page-head>
    <view class="uni-common-mt">
      <button v-for="(item, index) in fileList" :key="index" @click="openDocument(item)" :disabled="loading" class="uni-common-mt">
        打开 {{item.type}} 文件
      </button>
    </view>
  </view>
</template>

<script setup lang="uts">
type FileItem = {
  type: string,
  url: string
}

const title = 'openDocument'
const loading = ref(false)
const fileList = ref<Array<FileItem>>([
  {
    type: 'pdf',
    url: 'https://web-assets.dcloud.net.cn/unidoc/zh/helloworld.pdf'
  },
  {
    type: 'doc',
    url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.doc'
  },
  {
    type: 'docx',
    url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.docx'
  },
  {
    type: 'ppt',
    url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.ppt'
  },
  {
    type: 'pptx',
    url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.pptx'
  },
  {
    type: 'xls',
    url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.xls'
  },
  {
    type: 'xlsx',
    url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/helloworld.xlsx'
  }
])

const openDocument = (item: FileItem) => {
  loading.value = true
  uni.downloadFile({
    url: item.url,
    success: (res) => {
      uni.openDocument({
        filePath: res.tempFilePath,
        success: () => {
          console.log('打开文档成功')
        },
        fail: (err) => {
          console.log('打开文档失败',err)
        }
      })
    },
    fail: (err) => {
      console.log('下载失败',err)
    },
    complete: () => {
      loading.value = false
    }
  })
}
</script>

# 通用类型

# GeneralCallbackResult

名称 类型 必备 默认值 兼容性 描述
errMsg string -
错误信息