Vue sample code for online preview of office files

Vue sample code for online preview of office files

I'm working on electronic archives recently, and the backend provides the Huawei Cloud OSS link for the files. The function of clicking to download a file has been implemented. However, they hope that they can preview regular files directly without downloading them.

Logically speaking, to do online preview of files, you can just buy a third-party service, deploy the service on the back end, and connect it to the front end, and everything will be done.
If you can't withstand the third party, you will basically have to pay money. If you don't want to spend money, what other solutions are there?

Method 1

Preview online with Microsoft Office Online

https://view.officeapps.live.com/op/view.aspx?src=File address Example: https://view.officeapps.live.com/op/view.aspx?src=http://www.xxx.com/xxx.ppt

Method 2

Use the online preview of docx cloud service, the usage is similar to Microsoft

http://view.xdocin.com/xdoc?_xdoc=文件地址

The premise is that the file address provided by the backend must be a publicly accessible link. For example, here we upload the file to Huawei Cloud. The file can only be viewed, not edited.

The effect is as follows

insert image description here

insert image description here

insert image description here

On the code

    <!-- Preview Icon -->
                  <i
                    v-if="row.doc_url && canPreviewList.includes(row.doc_ext)"
                    style="font-weight: bold;font-size:16px;"
                    class="link-type el-icon-view"
                    @click.stop="previewFileEvent(row)"
                  />
    previewFileEvent(row) {
      const typeArr = ['doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx']
      let url = ''
      if (typeArr.indexOf(row.doc_ext) !== -1) {
        // Use Microsoft Office Online
        url = 'http://view.officeapps.live.com/op/view.aspx?src=' + row.doc_url
      } else {
        url = row.doc_url
      }
      // window.open() opens in the center const width = 1000; const height = 800
      const top = (window.screen.availHeight - height) / 2
      const left = (window.screen.availWidth - width) / 2
      window.open(url, '', 'width=' + width + ',height=' + height + ',top=' + top + ',left=' + left)
    }

I am using the service provided by Microsoft. Can be used to open 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx' files.
Some processing has been done on other files, such as PDF and image files, which can be opened directly through links, and the browser supports direct preview.
For other files that cannot be previewed, I have restricted them so that the preview icon is not displayed.

There is a problem here. The txt file is garbled when opened directly in the browser. The download was normal, no solution was found.
If anyone knows, please bring it along. O(∩_∩)O

The reason why the txt file is garbled has been found. It's related to the browser's encoding format.

The txt file I have here is saved in utf-8 encoding. But the default browser is not Google Chrome.

insert image description here

It needs to be modified to the corresponding encoding format for the display to be normal.

To modify the encoding format of Google Chrome, you need to install an official plug-in Set Character Encoding from the App Store
After installation, right-click on the page to modify the encoding format.

insert image description here

This is the end of this article about the sample code of Vue to realize online preview of office files. For more relevant Vue online preview of office content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue implements online preview of PDF files and downloading (pdf.js)
  • Vue implements online preview function of PDF document
  • Vue plugin development: How to use pdf.js to realize online preview of PDF documents on mobile phones
  • Vue implements online preview of PDF files (using pdf.js/iframe/embed)
  • vue Sample code for using vue-pdf to implement PDF online preview
  • Vue-pdf implements online preview of PDF files
  • vue-pdf realizes online file preview
  • Online preview of three common file types in Vue projects (pdf/word/excel tables)

<<:  Install Docker on Centos7 (2020 latest version available, just copy and paste)

>>:  An example of using CSS3 animation to achieve the effect of a circle expanding from small to large and spreading outward

Recommend

How to use domestic image warehouse for Docker

1. Problem description Due to some reasons, the d...

Some conclusions on developing mobile websites

The mobile version of the website should at least...

Element UI table realizes drop-down filtering function

This article example shares the specific code for...

MySql 8.0.11-Winxp64 (free installation version) configuration tutorial

1. Unzip the zip package to the installation dire...

A brief introduction to bionic design in Internet web design

When it comes to bionic design, many people will t...

MySQL 5.7.23 installation and configuration method graphic tutorial

This article records the installation tutorial of...

Methods and problems encountered in installing mariadb in centos under mysql

Delete the previously installed mariadb 1. Use rp...

Practice of Vue global custom instruction Modal drag

Table of contents background Implementation ideas...

Detailed explanation of flex layout in CSS

Flex layout is also called elastic layout. Any co...

Let's talk in depth about the principle and implementation of new in JS

Table of contents definition Constructor bodies a...

Linux system (Centos6.5 and above) installation jdk tutorial analysis

Article Structure 1. Preparation 2. Install Java ...

How to separate static and dynamic state by combining Apache with Tomcat

Experimental environment Apache and Tomcat are bo...