https://xxxx?file==${encodeURIComponent('http://xxxx.doc')}
属性 | 是否必填 | 值类型 | 默认值 | 说明 |
---|---|---|---|---|
fileUrl | 否 | String | 空 | 预览单个文档或视频传递url |
fileType | 是 | String | 空 | 类型(1.预览图片,2.预览文件,3.预览视频) |
imageList | 否 | Array | 空数组 | 预览单个或多个图片传递数组 |
###图片支持多张预览,所以传递数组进去
/uni_modules/ss-preview/hybrid/html/pdf-reader/index.html?file=${encodeURIComponent(value)}
改为/src/uni_modules/ss-preview/hybrid/html/pdf-reader/index.html?file=${encodeURIComponent(value)}
<template>
<view>
<ss-preview :fileUrl="fileUrl" :fileType="fileType" :imageList="imageList"></ss-preview>
</view>
</template>
<script>
export default {
data() {
return {
fileUrl: '',
fileType:'',
imageList:[],
}
},
onLoad(option) {
let options = JSON.parse(decodeURIComponent(option.item))
this.fileType = options.type;
if(options.type === '1') return this.imageList = options.src
this.fileUrl = options.src;
//此处仅为.doc,.docx,.xls,.xlsx示例,通过服务端接口把相关文件转换为pdf文件流
// let types = options.src.substr(options.src.lastIndexOf('.'));
// if(types !== '.pdf'){
// this.fileUrl = `https://xxxx?file==${encodeURIComponent(options.src)}`
// }
},
}
</script>
<template>
<view class="box">
<view style="margin-bottom:32rpx;">uni-app预览文件</view>
<view v-for="(item,index) in fileList" :key="index">
<view style="word-break: break-all;">{{item.src}}</view>
<button class="previewBtn" @click="toPage(item)">{{item.name}}</button>
</view>
</view>
</template>
<script>
export default {
components:{},
data() {
return {
fileList:[
{
type:'1',
name:'预览图片',
src:['https://bjetxgzv.cdn.bspapp.com/VKCEYUGU-uni-app-doc/efd8e280-60a9-11eb-a16f-5b3e54966275.jpg','https://bjetxgzv.cdn.bspapp.com/VKCEYUGU-uni-app-doc/efd8e280-60a9-11eb-a16f-5b3e54966275.jpg']
},
{
type:'2',
name:'预览文档',
src:'http://storage.xuetangx.com/public_assets/xuetangx/PDF/PlayerAPI_v1.0.6.pdf'
},
{
type:'3',
name:'预览视频',
src:'https://bjetxgzv.cdn.bspapp.com/VKCEYUGU-uni-app-doc/a876efc0-4f35-11eb-97b7-0dc4655d6e68.mp4',
},
]
};
},
onLoad(options) {},
methods: {
toPage(item){
if(item.type === '2'){//预览文档
//#ifdef MP-WEIXIN
//微信小程序预览文档可以直接打开,不用跳转页面
this.previewWechat(item.src)
return
//#endif
}
uni.navigateTo({
url:'/pages/preview?item='+encodeURIComponent(JSON.stringify(item))
})
},
//微信小程序预览文档,可预览.doc,.docx,.xls,.xlsx,.pdf等文件
previewWechat(urlPdf) {
uni.showLoading({
title: '正在加载中..'
})
uni.downloadFile({
url: urlPdf,
success: function(res) {
var filePath = res.tempFilePath;
uni.openDocument({
filePath: filePath,
showMenu: true,
success: function(res) {
console.log('打开文档成功');
uni.hideLoading()
},
});
},
complete: function(r) {
uni.hideLoading()
}
});
},
}
};
</script>
<style lang="scss" scoped>
.box {
padding: 20rpx 40rpx;
.previewBtn{
height:60rpx;
width:200rpx;
border-radius:16rpx;
background-color:#409eff;
color:#fff;
line-height:60rpx;
margin:24rpx 12rpx;
}
}
</style>