Vue2實現(xiàn)txt文件在線預(yù)覽的代碼示例
一、上傳完成后實現(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>實現(xiàn)效果:

二、后端提供文件下載接口實現(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ù)后將文件流進行地址轉(zhuǎn)換作為結(jié)果返回。
文件流轉(zhuǎn)url地址:
const blob = new Blob([content], { type: 'application/text' });
const url = window.URL.createObjectURL(blob);實現(xiàn)效果:

到此這篇關(guān)于Vue2實現(xiàn)txt文件在線預(yù)覽的代碼示例的文章就介紹到這了,更多相關(guān)Vue2 txt文件在線預(yù)覽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue兩組件間值傳遞 $router.push實現(xiàn)方法
兩組件間傳值,可能包含多種情況,這篇文章主要介紹了vue兩組件間值傳遞 $router.push實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
vue3.0中使用websocket,封裝到公共方法的實現(xiàn)
這篇文章主要介紹了vue3.0中使用websocket,封裝到公共方法的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
vue 路由緩存 路由嵌套 路由守衛(wèi) 監(jiān)聽物理返回操作
這篇文章主要介紹了vue 路由緩存 路由嵌套 路由守衛(wèi) 監(jiān)聽物理返回操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
解決Vue.js 2.0 有時雙向綁定img src屬性失敗的問題
下面小編就為大家分享一篇解決Vue.js 2.0 有時雙向綁定img src屬性失敗的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
vue中在echarts里設(shè)置的定時器清除不掉問題及解決
這篇文章主要介紹了vue中在echarts里設(shè)置的定時器清除不掉問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
基于vue3開發(fā)mobile-table適用于移動端表格
這篇文章主要給大家介紹了關(guān)于如何基于vue3開發(fā)mobile-table適用于移動端表格的相關(guān)資料,需要的朋友可以參考下2024-03-03
vuex實現(xiàn)數(shù)據(jù)狀態(tài)持久化
今天小編就為大家分享一篇vuex實現(xiàn)數(shù)據(jù)狀態(tài)持久化,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11

