最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue移動端實現(xiàn)pdf/excel/圖片在線預覽

 更新時間:2024年04月24日 11:24:51   作者:lory代碼搬運工  
這篇文章主要為大家詳細介紹了Vue移動端實現(xiàn)pdf/excel/圖片在線預覽功能的相關方法,文中的示例代碼講解詳細,有需要的小伙伴可以參考下

vue移動端實現(xiàn)pdf、excel、圖片在線預覽

一、首先預覽pdf

安裝vue-pdf,預覽excel安裝xlsx,預覽圖片是用的vant

npm install --save vue-pdf 
npm install xlsx --save
npm install vant --save

二、父頁面代碼

<template>
   <button @click="seeFile">
   </button>
</template>
//引入子頁面,也就是預覽的界面
import Views from './previewFile'
export default {
  name: 'FileList',
  components: { Views },
  data() {
    return {
      url:'',//要預覽的文件地址
      src: '', // 傳參
      type: '', // 附件類型
    }
  },
  methods: {
      //item參數(shù)為文件地址
      seeFile(this.url) {
      //文件預覽 
      if (item.fileName.toLowerCase().includes('jpg') || item.fileName.toLowerCase().includes('png')|| item.fileName.toLowerCase().includes('jpeg')) {
        this.src = fileAbsPath
        this.type = 'img'
      } else if (item.fileName.toLowerCase().includes('pdf')) {
        this.src = fileAbsPath
        this.type = 'pdf'
        this.$refs.vViews.pageNum = 1
      } else if (item.fileName.toLowerCase().includes('xls') || item.fileName.toLowerCase().includes('xlsx')) {
        this.src = fileAbsPath
        this.type = 'excel'
      }
      this.$nextTick(() => {
        this.$refs.Views.isShow = true
        this.$refs.Views.showFile(this.src)
      })
    },
  }
  }

三、子頁面previewFile(預覽頁面)代碼

<template>
  <div class="viewBox" v-if="isShow">
    <!-- pdf預覽 -->
    <div class="wrap-pdf" v-if="type === 'pdf'">
      <div class="pdf">
	  <div class="pdf-tab">
	    <div
	      class="btn-def"
	      @click.stop="prePage"><span>上一頁</span>
	    </div>
	    <div
	      class="btn-def"
	      @click.stop="nextPage"><span>下一頁</span>
	    </div>
	  </div>
	  <div style="text-align: center;">{{pageNum}}/{{pageTotalNum}}</div>
	  <div class="any-scroll-view">
	    <div ref="body">
	      <pdf
	        id="pdfPreview"
	        ref="pdf"
	        :src="pdfSrc"
	        :page="pageNum"
	        :rotate="pageRotate"
	        @password="password"
	        @progress="loadedRatio = $event"
	        @page-loaded="pageLoaded($event)"
	        @num-pages="pageTotalNum=$event"
	        @error="pdfError($event)"
	        @link-clicked="page = $event">
	      </pdf>
	    </div>
	  </div>
	</div>
      <!-- <pdf v-for="item in numPages" :key="item" :src="pdfSrc" :page="item"/> -->
    </div>
    <!-- 表格組件 -->
    <div class="table-box" v-if="type === 'excel'">
      <van-tabs
          class="table-tab"
          v-if="sheetNames.length"
          title-active-color="#07AC7F"
          color="#07AC7F"
          @click="clickTab">
          <van-tab
              v-for="(item, index) in sheetNames"
              :key="index"
              :name="item"
              :title="item"></van-tab>
      </van-tabs>
      <div class="tableBox" ref="excPreview"></div>
    </div>
		<!-- 關閉按鈕 -->
      <van-icon class="closeBtn" name="cross" @click="isShow = false" />
	</div>
</template>

<script>
import { ImagePreview } from 'vant'
import pdf from 'vue-pdf' 
import XLSX from 'xlsx'
export default {
  name: 'PreviewFile',
  components: {
    pdf
  },
  props: {
    datas: {},
    type: {}
  },
  data() {
    return {
      isShow: false,
      numPages: 1,
      pdfSrc: '',
      sheetNames: [],
      pageNum: 1,
      wsObj: {},
        pageTotalNum:1,
        pageRotate:0,
        // 加載進度
        loadedRatio:0,
        curPageNum:0,
    }
  },
  
  methods: {
    showFile(newVal) {
      console.log('----', newVal)
      if (this.type === 'img') {
        const that = this
        ImagePreview({
          images: [newVal],
          onClose() {
            that.isShow = false
          }
        })
      } else if (this.type === 'pdf') {
        this.pdfSrc = pdf.createLoadingTask(newVal)
      } else if (this.type === 'excel') {
        var xhr = new XMLHttpRequest()
        xhr.open('get', newVal, true)
        xhr.responseType = 'arraybuffer'
        let _this = this
        xhr.onload = function (e) {
          let that=this;
          var binary = "";
          if (xhr.status === 200) {
            var bytes  = new Uint8Array(xhr.response)
            var length = bytes.byteLength;
            for (var i = 0; i < length; i++) {
              binary += String.fromCharCode(bytes[i]);
            }
            var wb = XLSX.read(binary, { type: "binary" });
            var wsname = wb.SheetNames[0];
            var ws = wb.Sheets[wsname];
             _this.sheetNames = [...wb.SheetNames] // 數(shù)組
        _this.wsObj = { ...wb.Sheets }
        _this.changeExcel(_this.sheetNames[0])
          }
        }
        xhr.send()
      }
    },
    clickTab(name) {
      this.changeExcel(name)
    },
    prePage(){
        var p = this.pageNum
        p = p>1?p-1:this.pageTotalNum
        this.pageNum = p
      },
      nextPage(){
        var p = this.pageNum
        p = p<this.pageTotalNum?p+1:1
        this.pageNum = p
      },
      password(updatePassword, reason) {
        updatePassword(prompt('password is "123456"'))
      },
      pageLoaded(e){
        this.curPageNum = e
      },
      pdfError(error){
        console.error(error)
      },
    changeExcel(item) {
      // 獲取當前選中表格對象
      const ws = this.wsObj[item]
      console.log(ws);
      const keyArr = Object.keys(ws) || []
      const HTML = keyArr.length > 1 ? XLSX.utils.sheet_to_html(ws)
        : '<html><head><meta charset="utf-8"/>' +
                    '<title>SheetJS Table Export</title></head><body><div class="myTable">暫無數(shù)據(jù)</div></body>' +
                    '</html>'
      this.$nextTick(() => {
        this.$refs.excPreview.innerHTML = HTML
        // 獲取表格dom元素
        const tables = this.$refs.excPreview.children[2]
        // 添加完畢后 通過空格將數(shù)組組裝為字符串
        tables.className = 'myTable'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.excel-container {
  width: 100%;
}
table {
    display: table;
    border-collapse: collapse;
    box-sizing: border-box;
    border:  1px solid #929292;
    width: auto;height: auto;color: #333333;// 合并邊框
    th,tr{
      white-space: nowrap;overflow: hidden;text-overflow: ellipsis;background: #ffffff;padding: 10px;border:1px solid #929292;
      td{
        font-weight: normal;
        text-align: center;
        border:1px solid #929292;
      }
    }
}
.tableBox {width: 100vw;height: calc(100vh - 44px);overflow: auto;
}// 表格邊框
.pdf-tab {
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    padding: 0 .4rem;
    -ms-flex-pack: justify;
    justify-content: space-between;
}
.pdf-tab .btn-def {
    border-radius: .2rem;
    font-size: 0.5rem;
    // height: 40px;
    margin-top: 40px;
    // width: 6.4rem;
    text-align: center;
    // line-height: 1.93333rem;
    background: #409eff;
    color: #fff;
    // margin-bottom: 1.26667rem;
}
.pdf-total {
    text-align: center;
    font-size: 1.45333rem;
}
.pdf-process, .pdf-total {
    text-align: center;
    font-size: 1.45333rem;
}
.pdf-num {
    margin-bottom: 1.2rem;
}

.pdf-box, .word-box, .table-box, .txt-box {
  width: 100vw;
  height: 100vh;
}
.viewBox {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #ffffff;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 2000;
  .closeBtn {
    position: absolute;
      top: 10px;
      right: 10px;
      z-index: 10;
      font-size: 24px;
  }
}
</style>

缺點:excel預覽顯示不了邊框,word預覽還沒有找到好的解決方案!!!有找到word移動端預覽和解決了excel預覽邊框顯示的小伙伴可以私聊我。

效果圖如下:

以上就是Vue移動端實現(xiàn)pdf/excel/圖片在線預覽的詳細內(nèi)容,更多關于Vue在線預覽的資料請關注腳本之家其它相關文章!

相關文章

  • Vue中的transition封裝組件的實現(xiàn)方法

    Vue中的transition封裝組件的實現(xiàn)方法

    這篇文章主要介紹了Vue中的transition封裝組件的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • VueJS組件之間通過props交互及驗證的方式

    VueJS組件之間通過props交互及驗證的方式

    本篇文章主要介紹了VueJS組件之間通過props交互及驗證的方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-09-09
  • Vue3 Element Plus el-form表單組件示例詳解

    Vue3 Element Plus el-form表單組件示例詳解

    這篇文章主要介紹了Vue3 Element Plus el-form表單組件,Element Plus 是 ElementUI 的升級版,提供了更多的表單控件和功能,同時還改進了一些細節(jié)和樣式,本文結合示例代碼給大家詳細講解,需要的朋友可以參考下
    2023-04-04
  • Vue打包優(yōu)化之生產(chǎn)環(huán)境刪除console日志配置

    Vue打包優(yōu)化之生產(chǎn)環(huán)境刪除console日志配置

    這篇文章主要為大家介紹了Vue打包優(yōu)化之生產(chǎn)環(huán)境刪除console日志配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • Vue項目首屏性能優(yōu)化組件實戰(zhàn)指南

    Vue項目首屏性能優(yōu)化組件實戰(zhàn)指南

    Vue眾所周知是一個輕量級的框架,源碼僅僅為72.9KB,但是也有它自己的缺點,就是首屏加載會比較慢,這篇文章主要給大家介紹了關于Vue項目首屏性能優(yōu)化組件的相關資料,需要的朋友可以參考下
    2021-11-11
  • 關于vue編譯版本引入的問題的解決

    關于vue編譯版本引入的問題的解決

    這篇文章主要介紹了關于vue編譯版本引入的問題的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • Vue前端在線預覽文件插件使用詳解

    Vue前端在線預覽文件插件使用詳解

    文章介紹了兩種用于在線預覽文檔的插件:view.xdocin和view.officeapps.live,view.xdocin插件支持在線預覽并延長使用時間,但不支持下載,view.officeapps.live插件是微軟插件,但預覽速度較慢,且有時會出現(xiàn)翻頁問題,兩種插件都適用于不需要下載和安裝軟件的場景
    2026-01-01
  • vue 實現(xiàn)購物車總價計算

    vue 實現(xiàn)購物車總價計算

    今天小編就為大家分享一篇vue 實現(xiàn)購物車總價計算,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Vue插件報錯:Vue.js is detected on this page.問題解決

    Vue插件報錯:Vue.js is detected on this page.問題解決

    這篇文章主要介紹了Vue插件報錯:Vue.js is detected on this page.問題解決,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • Vite+Vue3.0+ElementPlus+axios+TS搭建項目全過程

    Vite+Vue3.0+ElementPlus+axios+TS搭建項目全過程

    文章主要介紹了如何使用Vite構建項目,包括創(chuàng)建項目、代碼規(guī)范(eslint、husky、prettti等等、EditorConfig配置等),項目配置(CSS預處理器、樣式重置、環(huán)境變量配置、axios封裝、本地存儲封裝)和開發(fā)生產(chǎn)環(huán)境配置等內(nèi)容
    2026-04-04

最新評論

宜章县| 淳化县| 额济纳旗| 玛曲县| 胶南市| 忻城县| 宜君县| 禄劝| 镶黄旗| 昌图县| 杭锦后旗| 宣化县| 黄骅市| 贡山| 温泉县| 固阳县| 湾仔区| 龙江县| 天峻县| 芒康县| 江陵县| 阳高县| 易门县| 西充县| 太仓市| 秦安县| 东阳市| 永仁县| 常山县| 沙坪坝区| 临颍县| 襄汾县| 张家界市| 太仆寺旗| 临武县| 张北县| 休宁县| 柞水县| 宁德市| 西城区| 信宜市|