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

Share 5 helpful CSS selectors to enrich your CSS experience

With a lot of CSS experience as a web designer, we...

Introduction to Apache deployment of https in cryptography

Table of contents Purpose Experimental environmen...

vue front-end HbuliderEslint real-time verification automatic repair settings

Table of contents ESLint plugin installation in H...

Detailed process of implementing the 2048 mini game in WeChat applet

Rendering Example Code Today we are going to use ...

Learn about CSS label display mode in one article

Tag type (display mode) HTML tags are generally d...

Detailed explanation of log processing of Docker containers

Docker has many log plug-ins. The default is to u...

MySql quick insert tens of millions of large data examples

In the field of data analysis, database is our go...

Completely delete MySQL steps

Table of contents 1. Stop MySQL Server first 2. U...

Problems with index and FROM_UNIXTIME in mysql

Zero, Background I received a lot of alerts this ...

The whole process of implementing the summary pop-up window with Vue+Element UI

Scenario: An inspection document has n inspection...

Detailed explanation of 8 ways to pass parameters in Vue routing components

When we develop a single-page application, someti...

Commands to find domain IP address in Linux terminal (five methods)

This tutorial explains how to verify the IP addre...