Vue2實(shí)現(xiàn)txt文件在線預(yù)覽的代碼示例
一、上傳完成后實(shí)現(xiàn)預(yù)覽
<template> <!-- txt文件預(yù)覽 --> <div> <el-dialog title="文件預(yù)覽" :visible="dialogVisible" show-close append-to-body width="60%" :before-close="cancelDialog" > <input type="file" @change="handleFileChange"> <div class="panel-box" v-html="txt"></div> <div slot="footer" class="dialog-footer tc"> <button class="btn-submit btn-submit-sm" @click="cancelDialog()">關(guān)閉</button> </div> </el-dialog> </div> </template> <script> export default { name:'txtFilePreview', data() { return { dialogVisible:false, txt:'', }; }, methods: { previewFile(){ this.dialogVisible = true; }, handleFileChange(e){ const file = e.target.files[0]; if (!file) { return; } const that = this; const reader = new FileReader(); reader.onload = function(e) { that.txt = e.target.result.split('\n').join('<br />'); }; reader.onerror = function(error) { console.error('Error: ', error); }; reader.readAsText(file); }, cancelDialog(){ this.dialogVisible = false; }, } }; </script> <style lang="scss" scoped> </style>
實(shí)現(xiàn)效果:
二、后端提供文件下載接口實(shí)現(xiàn)預(yù)覽
<template> <!-- txt文件預(yù)覽 --> <div> <el-dialog title="文件預(yù)覽" :visible="dialogVisible" show-close append-to-body width="60%" :before-close="cancelDialog" > <div class="panel-box" v-html="txt"></div> <div slot="footer" class="dialog-footer tc"> <button class="btn-submit btn-submit-sm" @click="cancelDialog()">關(guān)閉</button> </div> </el-dialog> </div> </template> <script> export default { name:'txtFilePreview', data() { return { dialogVisible:false, txt:'', }; }, methods: { previewFile(event,docId) { event.stopPropagation(); this.dialogVisible = true; const inParam = { DOC_ID: docId, STAFF_ID: this.$store.getters.staffId, STAFF_NAME: this.$store.getters.staffName, SYS_USER_CODE: this.$store.getters.systemUserCode }; const loading = this.$commonUtil.loading.open() this.$txtPreview(this.mciApi.common.file.downFile, { ...inParam }).then(r => { loading.close() // 根據(jù)文件地址解析txt文件內(nèi)容 this.$axios.get(r,{ responseType:"blob", transformResponse: [ async (data)=>{ return await this.transformData(data); }, ], }).then(res=>{ res.data.then((data)=>{ this.txt = data ? data.split('\n').join('<br />') : ''; }) }) }).catch((e) => { loading.close() }) }, transformData(data){ return new Promise((resolve)=>{ let reader = new FileReader(); reader.readAsText(data,'UTF-8'); reader.onload = ()=>{ resolve(reader.result) } }) }, cancelDialog(){ this.dialogVisible = false; }, } }; </script> <style lang="scss" scoped> </style>
Tips:
$txtPreview:是封裝后的post請求;
this.mciApi.common.file.downFile:是后端提供的文件下載方法,返回一個文件流數(shù)據(jù)。獲取數(shù)據(jù)后將文件流進(jìn)行地址轉(zhuǎn)換作為結(jié)果返回。
文件流轉(zhuǎn)url地址:
const blob = new Blob([content], { type: 'application/text' }); const url = window.URL.createObjectURL(blob);
實(shí)現(xiàn)效果:
到此這篇關(guān)于Vue2實(shí)現(xiàn)txt文件在線預(yù)覽的代碼示例的文章就介紹到這了,更多相關(guān)Vue2 txt文件在線預(yù)覽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3中v-for報錯'item'is?of?type'unknown'的
在寫vue3+ts的項目,得到一個數(shù)組,需要循環(huán)展示,使用v-for循環(huán),寫完之后發(fā)現(xiàn)有個報錯,接下來通過本文給大家介紹vue3中v-for報錯?‘item‘?is?of?type?‘unknown‘的解決方法,感興趣的朋友一起看看吧2023-11-11vue 需求 data中的數(shù)據(jù)之間的調(diào)用操作
這篇文章主要介紹了vue 需求 data中的數(shù)據(jù)之間的調(diào)用操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08Vue3使用hooks函數(shù)實(shí)現(xiàn)代碼復(fù)用詳解
這篇文章主要介紹了Vue3使用hooks函數(shù)實(shí)現(xiàn)代碼復(fù)用詳解,Vue3的hook函數(shù)可以幫助我們提高代碼的復(fù)用性,?讓我們能在不同的組件中都利用hooks函數(shù)2022-06-06vue-cli的build的文件夾下沒有dev-server.js文件配置mock數(shù)據(jù)的方法
這篇文章主要介紹了vue-cli的build的文件夾下沒有dev-server.js文件配置mock數(shù)據(jù)的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04對Vue- 動態(tài)元素屬性及v-bind和v-model的區(qū)別詳解
今天小編就為大家分享一篇對Vue- 動態(tài)元素屬性及v-bind和v-model的區(qū)別詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08