纯前端上传word,xlsx,ppt,在前端预览并下载成图片(预览效果可以,下载图片效果不太理想)

纯前端上传word,xlsx,ppt,在前端预览并下载成图片(预览效果可以,下载图片效果不太理想)

预览效果链接: https://github.com/501351981/vue-office

插件文档链接: https://501351981.github.io/vue-office/examples/docs/config/

一.安装依赖

javascript 复制代码
//docx文档预览组件
npm install @vue-office/docx vue-demi
 
//excel文档预览组件
npm install @vue-office/excel vue-demi
 
//pdf文档预览组件
npm install @vue-office/pdf vue-demi
//html生成图片
npm install html2canvas

二、主要代码

javascript 复制代码
<template>
    <div class="index">
        <div class="select-file">
            <input id="input" type="file" />
        </div>
        <div class="file-preview" ref="excelContainer">
            <!-- <VueOfficeDocx v-if="src" style="height: 600px;" :src="src" /> -->
            <VueOfficeExcel class="file-content" v-if="src" style="height: 1280px" :src="src" />
            <!-- <VueOfficePdf v-if="src" style="height: 600px" :src="src" /> -->
        </div>
        <button @click="generateImage">Generate Image</button>
    </div>
</template>
<script>
// import VueOfficeDocx from '@vue-office/docx'
// import '@vue-office/docx/lib/index.css'

import VueOfficeExcel from '@vue-office/excel'
import '@vue-office/excel/lib/index.css'

// import VueOfficePdf from '@vue-office/pdf'

import html2canvas from 'html2canvas'
export default {
    data() {
        return {
            src: '',
        }
    },
    components: {
        // VueOfficeDocx,
        VueOfficeExcel,
        //  VueOfficePdf
    },
    mounted() {
        this.addInputEventListener()
    },
    methods: {
        /**
         * application/msword;charset=utf-8
         * application/pdf;charset=utf-8
         * application/vnd.ms-excel
         */
        addInputEventListener() {
            const input = document.querySelector('#input')
            input.addEventListener('input', e => {
                const fileBlob = e.target.files[0]

                // 第一种方式(通过window.URL.createObjectURL将Blob文件流转为一个路径)
                this.src = window.URL.createObjectURL(new Blob([fileBlob]))

                // 第二种方式(转为base64编码)
                const fileReader = new FileReader()
                fileReader.readAsDataURL(fileBlob)
                fileReader.onload = e => {
                    this.src = e.target.result
                }

                // 第三种方式(获取到buffer)
                fileBlob.arrayBuffer().then(buffer => {
                    this.src = buffer
                })
            })
        },
        generateImage() {
            const element = this.$refs.excelContainer
            html2canvas(element, { useCORS: true })
                .then(canvas => {
                    const image = canvas.toDataURL('image/png')
                    const link = document.createElement('a')
                    link.href = image
                    link.download = 'excel_image.png'
                    link.click()
                })
                .catch(error => {
                    console.error('Error generating image:', error)
                })
        },
    },
}
</script>
<style scoped>
.index {
    width: 100%;
    height: 100%;
    padding: 15px;
    box-sizing: border-box;
}
.select-file {
    width: 100%;
    height: 35px;
    border-bottom: 1px dashed #ccc;
    margin-bottom: 15px;
}
.file-preview {
    width: 100%;
    height: 100%;
    border: 1px dashed #007acc;
}
</style>

没了~

链接: https://www.jb51.net/article/278400.htm

相关推荐
谙忆10241 小时前
前端图片直传对象存储:OSS/S3 预签名 URL、STS 临时凭证与回调校验
前端
CHNE_TAO_EMSM2 小时前
Android studio 打开文件时自动下载源码
前端·javascript·android studio
_清风来叙2 小时前
【软件】Word软件不支持显示emoji表情包如何解决
电脑·word
一孤程3 小时前
Airtest自动化测试第五篇:小程序与Web测试——跨平台自动化全覆盖
前端·自动化测试·小程序·自动化·测试·airtest
IT_陈寒3 小时前
SpringBoot自动配置不是你以为的那样的智能
前端·人工智能·后端
yume_sibai4 小时前
大屏数据可视化 - 边框红绿呼吸灯实现详解
前端·信息可视化·typescript
ZZZMMM.zip4 小时前
演示架构师-PPT大纲生成的HarmonyOS开发实践
人工智能·华为·powerpoint·harmonyos·鸿蒙·鸿蒙系统
Hyyy4 小时前
很多Desktop都在上的Computer Use是什么
前端·llm
慢功夫4 小时前
开篇:VS Code 为什么不是一个普通 React App
前端·visual studio code
你挚爱的强哥5 小时前
Vue2 实现 1.5s 十连击监听(连续点击若干次),封装通用可复用点击检测工具,不用 data!Vue 封装通用连击监听方法,支持自定义时长与点击次数
前端·javascript·vue.js