当前位置: 首页 > news >正文

uniapp图片文档预览

1、新建pages/webview/pdfView.vue

<template>
    <view>
        <web-view :src="url"></web-view>
    </view>
</template>

<script>    
    export default {
        data() {
            return {
                url: '',
            };
        },
        onLoad(params) {
            // console.log("params:"+JSON.stringify(params));
            let {
                url
            } = params
            this.url = `/static/pdf.html?url=${url}`
        },
    }
</script>
 

2、查看文件

<button class="uni-button" size="mini" type="info" @click="previewPdf(item)">查看</button>


previewPdf(obj) {
    if (obj.attachmentPath != '' && obj.attachmentPath != null) {
        let fileUrl = baseUrl + obj.attachmentPath;
        console.log("==fileUrl==https://www.×××.com/profile/upload/123.jpg");
        console.log("==obj.attachmentPath==/profile/upload/123.jpg");
        const imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp'];
        let suffix = obj.attachmentPath.split('.').pop().toLowerCase();
        if (imageExtensions.includes(suffix)) {
            const imageList = [fileUrl];
            uni.previewImage({
                urls: imageList,
                current: imageList[0],
            });
        } else if (suffix === 'doc' || suffix === 'docx') {
            // H5
            const url = `https://view.officeapps.live.com/op/view.aspx?src=${encodeURIComponent(fileUrl)}`
            window.open(url);

            // 小程序
            uni.showLoading({
                title: '正在加载中...'
            })
            uni.downloadFile({
                url: fileUrl,
                success: function(res) {
                    var filePath = res.tempFilePath;
                    uni.openDocument({
                        filePath: filePath,
                        showMenu: true,
                        success: function(res) {
                            console.log('打开文档成功');
                            uni.hideLoading()
                        },
                        fail(err) {
                            console.log(err)
                            uni.hideLoading()
                        }
                    });
                },
                fail(err) {
                    console.log(err)
                    uni.hideLoading()
                }
            });
        } else if(suffix === 'pdf') {
            this.$tab.navigateTo('/pages/filePreview/pdfView?url=' + fileUrl);
        }
    } else {
        uni.showToast({
            title: '文件地址缺失',
            icon: "error"
        });
    }
},

相关文章:

  • uniappx插件nutpi-idcard 开发与使用指南(适配鸿蒙)
  • uniapp微信小程序视频实时流+pc端预览方案
  • 【新品解读】一板多能,AXRF49 定义新一代 RFSoC FPGA 开发平台
  • SFTP与SSH分离
  • 小知识点一:无刷电机
  • 抑郁症患者数据分析
  • 记一次spark在docker本地启动报错
  • Dagger依赖注入框架的介绍
  • Eureka REST 相关接口
  • Spring Boot 缓存注解详解:@Cacheable、@CachePut、@CacheEvict(超详细实战版)
  • Kafka 安装教程(支持 Windows / Linux / macOS)
  • Ubantu-Docker配置最新镜像源250605
  • 鸿蒙jsonToArkTS_工具exe版本来了
  • 从零到一:Maven 快速入门教程
  • OpenAI 即将推出 GPT-5:开启多模态、持续记忆对话新时代
  • leetcode1519. 子树中标签相同的节点数- medium
  • LFWG2024.08
  • 新版NANO下载烧录过程
  • 在 CentOS 上将 Ansible 项目推送到 GitHub 的完整指南
  • 使用Python提取PDF元数据的完整指南
  • 什么是网站的备案号/汕头seo管理
  • 小小影视大全在线观看免费观看/简述seo对各类网站的作用
  • 智慧团建电脑版登录/优化关键词有哪些方法
  • 未央区政府网站建设/怎样在百度上打广告
  • 梁平网站建设/网站一年了百度不收录
  • 怎样做网站平台赚钱/全自动推广引流软件