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

Vue使用OnlyOffice预览文档方案

  1. 服务器安装OnlyOffice ~~ 找后台或运维
  2. OfficePreview.vue
<template>
  <div id="officePreview"></div>
</template>

<script setup>
import { onMounted } from "vue";

const props = defineProps({
  previewUrl: {
    type: String,
    default: "",
  },
  previewName: {
    type: String,
    default: "文件名字",
  },
});

const getInfo = (url, name) => {
  var decodedUrl = decodeURIComponent(url);
  var path = new URL(decodedUrl).pathname;
  var filename = name || path.split("/").pop();
  var fileExtension = filename.split(".").pop();
  var documentType;
  var paths = path.split("/");
  var key = paths[paths.length - 2]; // key唯一,由字母数字组成。
  if (
    [
      "djvu",
      "doc",
      "docm",
      "docx",
      "docxf",
      "dot",
      "dotm",
      "dotx",
      "epub",
      "fb2",
      "fodt",
      "htm",
      "html",
      "mht",
      "mhtml",
      "odt",
      "oform",
      "ott",
      "oxps",
      "pdf",
      "rtf",
      "stw",
      "sxw",
      "txt",
      "wps",
      "wpt",
      "xml",
      "xps",
    ].includes(fileExtension.toLowerCase())
  ) {
    documentType = "word";
  } else if (
    [
      "csv",
      "et",
      "ett",
      "fods",
      "ods",
      "ots",
      "sxc",
      "xls",
      "xlsb",
      "xlsm",
      "xlsx",
      "xlt",
      "xltm",
      "xltx",
      "xml",
    ].includes(fileExtension.toLowerCase())
  ) {
    documentType = "cell";
  } else if (
    [
      "dps",
      "dpt",
      "fodp",
      "odp",
      "otp",
      "pot",
      "potm",
      "potx",
      "pps",
      "ppsm",
      "ppsx",
      "ppt",
      "pptm",
      "pptx",
      "sxi",
    ].includes(fileExtension.toLowerCase())
  ) {
    documentType = "slide";
  } else {
    documentType = "unknown";
  }
  return {
    filename: filename,
    fileExtension: fileExtension,
    documentType: documentType,
    key: key,
  };
};

const сonnectEditor = () => {
  console.log("0000123", props.previewUrl);
  const { filename, fileExtension, documentType, key } = getInfo(
    props.previewUrl,
    props.previewName
  );

  var config = {
    document: {
      fileType: fileExtension,
      key: key,
      title: props.previewName || filename,
      url: props.previewUrl,
    },
    documentType: documentType,
    editorConfig: {
      mode: "view",
    },
    height: "100%",
    width: "100%",
  };
  console.log(config);
  // eslint-disable-next-line
  new DocsAPI.DocEditor("officePreview", config);
};

onMounted(() => {
  if (typeof DocsAPI === "undefined") {
    const script = document.createElement("script");
    script.src =
      process.env.VUE_APP_ONLYOFFICE_DOCUMENTSERVER +
      "/web-apps/apps/api/documents/api.js";
    script.async = true;
    script.addEventListener("load", сonnectEditor);

    document.head.appendChild(script);
  } else {
    сonnectEditor();
  }
});
</script>
  1. 定义process.env.VUE_APP_ONLYOFFICE_DOCUMENTSERVER
// .env.dev
VUE_APP_FILE_PATH = 'http://192.168.3.208/doc-info'
VUE_APP_ONLYOFFICE_DOCUMENTSERVER = 'http://192.168.3.208:7073'
  1. 路由
{
	path: "office-preview",
	name: "office-preview",
	component: () => import("@/components/office-preview/index.vue"),
},
  1. 预览函数
import router from "@/router";

// 预览文件
export const handlePreview = (file) => {
  if (!file) {
    return;
  }

  const name = file.documentName;
  const url = (process.env.VUE_APP_FILE_PATH || "") + file.documentUrl;
  if (!name || !url || !router) return;

  const lowerCaseName = name.toLowerCase();
  const suffixName = "." + lowerCaseName.split(".").pop();
  // 1. 处理文本和文档类型
  if ([".txt", ".doc", ".docx", ".pdf"].includes(suffixName)) {
    let routeUrl = router.resolve({
      name: "office-preview",
      query: { url, name },
    });
    window.open(routeUrl.href, "_blank");
    return;
  }
  // 2. 处理图片和视频类型
  if (
    [".png", ".jpg", ".jpeg", ".bmp", ".gif", ".mp4", ".mov"].includes(
      suffixName
    )
  ) {
    window.open(url, "_blank");
    return;
  }
  // 3. 提示不支持的文件类型
  ElMessage({
    message: "暂不支持该格式文件预览",
    type: "warning",
  });
};
  1. 调试经验
    a. http://192.168.3.205:7073/web-apps/apps/api/documents/api.js 是否能正常访问
    b. http://192.168.3.205:7073 是否能正常上传预览文件
    c. 嵌入后,报这个错表示跨域,请找对应人:Access to fetch at 'http://192.168.3.205:7073/web-apps/apps/api/documents/api.js' from origin 'http://192.168.3.208' has been blocked by CORS policy
    d. 检查文件key是否唯一且为纯字母数字格式。否则导致预览出不来。

http://www.kler.cn/news/360245.html

相关文章:

  • uniapp展示本地swf格式文件,实现交互
  • WPF开发之页面切换
  • 【氮化镓】低温对p-GaN HEMT迁移率、阈值电压和亚阈值摆幅的影响
  • 云安全 | AWS S3存储桶安全设计缺陷分析
  • 人工智能 | 阿里通义千问大模型
  • 面试篇!!!!!
  • 数据结构编程实践20讲(Python版)—20并查集
  • 超全!一文详解大模型的11种微调方法
  • 【数据结构】预备练习——文件读写
  • Java集合剖析4】LinkedList
  • 大模型生图安全疫苗注入——进阶解决方案与系统优化(DataWhale组队学习)
  • 【Flutter】状态管理:Provider状态管理
  • Nest.js 实战 (十五):前后端分离项目部署的最佳实践
  • 工业大模型:体系架构、关键技术与典型应用
  • 大数据之——Hadoop的HDFS、YARN、MapReduce
  • LLM应用实战: OpenAI多代理新作-Swarm
  • postgresql执行计划解读案例
  • 03_深入理解Linux:系统组成、内核版本及文件系统详解
  • jiehun_DEMO
  • without OpenSSL