
简体中文
打开文档
Web | 微信小程序 | Android | iOS | HarmonyOS |
---|---|---|---|---|
x | 4.41 | - | - | 4.61 |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | OpenDocumentOptions | 否 | - | - | uni.openDocument参数定义 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
该 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>
该 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>
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
errMsg | string | 是 | - | 错误信息 |