pdf以及其他文件预览

vue3实现文件预览,其实还是要下载,先下载下来然后实现预览,代码:

<previewFile
            :fileUrl="fileUrl"
            :fileType="fileTypeDetail"
            v-model:visible="previewVisible"
        />
//点击事件触发下载预览
const appraisalProgram = () => {
    handlePreview({ attachment: 'indicator-management/mass-production/dashboard/考核方案.pdf' });
};

const fileUrl = ref('');
const previewVisible = ref(false);
const fileTypeDetail = ref('');
// 附件预览
const handlePreview = async (obj) => {
    const fileType = obj.attachment.slice(obj.attachment.lastIndexOf('.') + 1).toLowerCase();
    fileTypeDetail.value = fileType;
    let fileName = obj.attachment;
    const regex = /\.(\w+)$/;
    const match = fileName.match(regex);
    const type = match ? match[1].toLocaleUpperCase() : '';
    const imgType = ['PNG', 'IMG', 'JPG', 'JPEG', 'GIF', 'SVG'];
    const pdfType = ['PDF'];
    commonApis
        .exportTemplate({
            filePath: fileName
        })
        .then((res) => {
            if (imgType.includes(type)) {
                const blob = new Blob([res.data], {
                    type: 'image/jpeg' || 'image/png'
                });
                const url = window.URL.createObjectURL(blob);
                // window.open(url, '_blank');  //如果打开新页面可以直接用
                fileUrl.value = url;
                previewVisible.value = true;  //这里是打开了一个抽屉组件
            } else if (pdfType.indexOf(type) > -1) {
                const blob = new Blob([res.data], {
                    type: 'application/pdf'
                });
                const url = window.URL.createObjectURL(blob);
                fileUrl.value = url;
                previewVisible.value = true;
                // window.open(url, '_blank');  //如果打开新页面可以直接用
            }
        });
};

关于previewFile组件也小小的展示下,其实就是用插件展示不同类型的文件:

<template>
    <ghy-drawer
        :width="'calc(100vw - 208px)'"
        title="文件预览"
        v-bind="$attrs"
        @cancel="onClose"
        :footer="false"
        unmountOnClose
    >
        <div
            v-if="
                props.fileType === 'jpg' ||
                props.fileType === 'png' ||
                props.fileType === 'jpeg' ||
                props.fileType === 'JPG' ||
                props.fileType === 'PNG' ||
                props.fileType === 'JPEG' ||
                props.fileType === 'svg'
            "
        >
            <img :src="props.fileUrl" style="width: 80%; height: 80%; margin: auto" />
        </div>
        <div v-if="props.fileType === 'pdf'">
            <VueOfficePdf
                :src="props.fileUrl"
                @rendered="renderedHandler"
                @error="errorHandler"
            ></VueOfficePdf>
        </div>
        <div v-if="props.fileType === 'doc' || props.fileType === 'docx'">
            <VueOfficeDocx :src="props.fileUrl"></VueOfficeDocx>
        </div>
        <div v-if="props.fileType === 'xls' || props.fileType === 'xlsx'">
            <VueOfficeExcel :src="props.fileUrl"></VueOfficeExcel>
        </div>
    </ghy-drawer>
</template>

<script setup>
import VueOfficePdf from '@vue-office/pdf';
import VueOfficeExcel from '@vue-office/excel';
import VueOfficeDocx from '@vue-office/docx';
import '@vue-office/docx/lib/index.css';
const emits = defineEmits(['update:visible']);

const onClose = () => {
    emits('update:visible', false);
};
const props = defineProps({
    fileUrl: {
        type: String,
        default: ''
    },
    fileType: {
        type: String,
        default: ''
    }
});

const renderedHandler = () => {
    console.log('渲染完成');
};
const errorHandler = () => {
    console.log('渲染失败');
};

watch(
    () => props.fileUrl,
    (value) => {
        console.log('>>>>>>>>>>>>>>>>>>>>>>>fileType', props.fileType);
        console.log('>>>>>>>>>>>>>>>>>>>>>>>fileUrl.value', props.fileUrl);
    }
);
emits('update:visible', false);
</script>

<style lang="less" scoped></style>

评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值