Back to Uni App

Open Document

docs/api/open-document.md

2.3.312.6 KB
Original Source
<!-- ## uni.openDocument(options?) @opendocument -->

::: sourceCode

uni.openDocument(options?) @opendocument

GitCode: https://gitcode.com/dcloud/uni-api/tree/alpha/uni_modules/uni-openDocument

GitHub: https://github.com/dcloudio/uni-api/tree/alpha/uni_modules/uni-openDocument

:::

打开文档

openDocument 兼容性

Web微信小程序AndroidiOSiOS uni-app x UTS 插件HarmonyOSHarmonyOS(Vapor)
<a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a>4.414.714.714.714.615.0

参数

名称类型必填默认值兼容性描述
optionsOpenDocumentOptions-Web: x; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -uni.openDocument参数定义

options 的属性描述

名称类型必备默认值兼容性描述
filePathstring-Web: x; 微信小程序: 4.41; Android: 4.71; iOS: 4.71; iOS uni-app x UTS 插件: 4.71; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0文件路径,仅支持本地路径
fileTypestring-Web: x; 微信小程序: 4.41; Android: 4.71; iOS: 4.71; iOS uni-app x UTS 插件: 4.71; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0文件类型,指定文件类型打开文件,微信小程序仅支持类型:doc, xls, ppt, pdf, docx, xlsx, pptx,App端由系统打开,原则上可以打开任意文件;
success(res: OpenDocumentSuccess) => void-Web: x; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -uni.openDocument成功回调函数定义
fail(res: OpenDocumentFail) => void-Web: x; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -uni.openDocument失败回调函数定义
complete(res: any) => void-Web: x; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -uni.openDocument完成回调函数定义
showMenuboolean-Web: x; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -需要基础库: 2.11.0

是否显示右上角菜单 |

OpenDocumentFail 的属性值 @opendocumentfail-values

名称类型必备默认值兼容性描述
errCodenumber-Web: x; 微信小程序: 4.41; Android: 4.71; iOS: 4.71; iOS uni-app x UTS 插件: 4.71; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0错误码
errSubjectstring-Web: x; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -统一错误主题(模块)名称
dataany-Web: x; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -错误信息中包含的数据
causeError-Web: x; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -源错误信息,可以包含多个错误,详见SourceError
errMsgstring-Web: x; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -

errCode 的属性描述

合法值兼容性描述
1300601Web: x; 微信小程序: 4.41; Android: 4.71; iOS: 4.71; iOS uni-app x UTS 插件: 4.71; HarmonyOS: 4.71; HarmonyOS(Vapor): 5.0路径无效
1300602Web: x; 微信小程序: 4.41; Android: 4.71; iOS: 4.71; iOS uni-app x UTS 插件: 4.71; HarmonyOS: 4.71; HarmonyOS(Vapor): 5.0文件不存在
1300603Web: x; 微信小程序: 4.41; Android: 4.71; iOS: 4.71; iOS uni-app x UTS 插件: 4.71; HarmonyOS: 4.71; HarmonyOS(Vapor): 5.0不支持该文件类型
1300604Web: x; 微信小程序: 4.41; Android: 4.71; iOS: 4.71; iOS uni-app x UTS 插件: 4.71; HarmonyOS: 4.71; HarmonyOS(Vapor): 5.0其他未知错误

示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见

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

::: preview

appRedirect https://hellouniappx.dcloud.net.cn/appredirect.html?path=pages/API/open-document/open-document

uvue
<template>
    <page-head :title="title"></page-head>
    <!-- #ifdef APP -->
    <scroll-view direction="vertical" style="flex:1">
    <!-- #endif -->
      <view class="uni-common-mt">
        <button v-for="(item, index) in fileList" :key="index" @click="openDocument(item)" style="margin: 10px;">
          打开 {{item.type}} 文件
        </button>
      </view>
    <!-- #ifdef APP -->
    </scroll-view>
    <!-- #endif -->
</template>

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

  const title = 'openDocument'
  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'
    },
    {
      type: 'zip',
      url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/to.zip'
    },
    {
      type: 'br',
      url: '/static/filemanager/1.txt.br'
    },
    {
      type: 'mp3',
      url: '/static/test-audio/ForElise.mp3'
    },
    {
      type: 'mp4',
      url: '/static/test-video/10second-demo.mp4'
    },
    {
      type: 'svg',
      url: '/static/test-image/logo.svg'
    }
  ])

  const openDocument = (item : FileItem) => {

    if (item.url.startsWith('http')) {
      uni.showLoading({
        title: '下载中',
        mask: true
      })
      uni.downloadFile({
        url: item.url,
        success: (res) => {
          uni.openDocument({
            filePath: res.tempFilePath,
            success: () => {
              uni.hideLoading()
              console.log('打开文档成功')
            },
            fail: (err) => {
              uni.hideLoading()
              console.log('打开文档失败', err)
              uni.showToast({
                title: '错误码:' + err.errCode.toString(),
                icon: "error"
              })
            }
          })
        },
        fail: (err) => {
          uni.hideLoading()
          console.log('下载失败', err)
          uni.showToast({
            title: '下载失败:' + err.errCode.toString(),
            icon: "error"
          })
        }
      })
    } else {
      uni.openDocument({
        filePath: item.url,
        success: () => {
          console.log('打开文档成功')
        },
        fail: (err) => {
          console.log('打开文档失败', err)
          uni.showToast({
            title: '错误码:' + err.errCode.toString(),
            icon: "error"
          })
        }
      })
    }


  }
</script>

:::

参见

示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见

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

::: preview

appRedirect https://hellouniappx.dcloud.net.cn/appredirect.html?path=pages/API/open-document/open-document

uvue
<template>
    <page-head :title="title"></page-head>
    <!-- #ifdef APP -->
    <scroll-view direction="vertical" style="flex:1">
    <!-- #endif -->
      <view class="uni-common-mt">
        <button v-for="(item, index) in fileList" :key="index" @click="openDocument(item)" style="margin: 10px;">
          打开 {{item.type}} 文件
        </button>
      </view>
    <!-- #ifdef APP -->
    </scroll-view>
    <!-- #endif -->
</template>

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

  const title = 'openDocument'
  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'
    },
    {
      type: 'zip',
      url: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/file/to.zip'
    },
    {
      type: 'br',
      url: '/static/filemanager/1.txt.br'
    },
    {
      type: 'mp3',
      url: '/static/test-audio/ForElise.mp3'
    },
    {
      type: 'mp4',
      url: '/static/test-video/10second-demo.mp4'
    },
    {
      type: 'svg',
      url: '/static/test-image/logo.svg'
    }
  ])

  const openDocument = (item : FileItem) => {

    if (item.url.startsWith('http')) {
      uni.showLoading({
        title: '下载中',
        mask: true
      })
      uni.downloadFile({
        url: item.url,
        success: (res) => {
          uni.openDocument({
            filePath: res.tempFilePath,
            success: () => {
              uni.hideLoading()
              console.log('打开文档成功')
            },
            fail: (err) => {
              uni.hideLoading()
              console.log('打开文档失败', err)
              uni.showToast({
                title: '错误码:' + err.errCode.toString(),
                icon: "error"
              })
            }
          })
        },
        fail: (err) => {
          uni.hideLoading()
          console.log('下载失败', err)
          uni.showToast({
            title: '下载失败:' + err.errCode.toString(),
            icon: "error"
          })
        }
      })
    } else {
      uni.openDocument({
        filePath: item.url,
        success: () => {
          console.log('打开文档成功')
        },
        fail: (err) => {
          console.log('打开文档失败', err)
          uni.showToast({
            title: '错误码:' + err.errCode.toString(),
            icon: "error"
          })
        }
      })
    }


  }
</script>

:::

通用类型

GeneralCallbackResult

名称类型必备默认值兼容性描述
errMsgstring-Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -错误信息