vue中預(yù)覽zip的實(shí)現(xiàn)示例
zip的數(shù)據(jù)流從后端接口獲取為blob形式,使用插件jszip, 提取出zip中的目錄并綁定到a-tree中。
同時(shí)根據(jù)壓縮包中文件的類(lèi)型來(lái)修改tree中的icon,并修改樣式使顯示更加清晰。
1. 添加jszip插件:
yarn add jszip
(我一般都使用yarn添加,添加的時(shí)候很穩(wěn)定快速,很少出問(wèn)題)
2. 由于我的項(xiàng)目中只是某個(gè)頁(yè)面使用到該功能,所以使用局部引用:
在使用該功能的頁(yè)面引入:
import JSZip from 'jszip'
3. 頁(yè)面設(shè)計(jì),我使用了a-tree來(lái)展示,主要用到了treeData數(shù)據(jù):
格式如下:
const treeData = [
{
title: "root",
key: "",
scopedSlots: { icon: "folder" },
children: [
{
title: "folder1",
key: "0-1",
id:"0-1",
parentId:"",
scopedSlots: { icon: "folder" },
children: [
{ title: "1.txt", key: "0-1-1", parentId:"0-1",scopedSlots: { icon: "txt" } },
{ title: "3.png", key: "0-1-2", parentId:"0-1",scopedSlots: { icon: "png" } },
{ title: "2.xml", key: "0-1-3", parentId:"0-1", scopedSlots: { icon: "xml" } },
],
},
{
title: "folder2",
key: "0-2",
id:"0-2",
parentId:"",
scopedSlots: { icon: "folder" },
children: [
{ title: "7.xlsx", key: "0-2-1", parentId:"0-2",scopedSlots: { icon: "childEdit" } },
],
},
{
title: "6.pdf",
key: "0-3",
id:"0-3",
parentId:"",
scopedSlots: { icon: "pdf" },
},
],
},
];a--tree的html如下:
<a-tree
showIcon
:default-expanded-keys="expandedKeys"
:expandedKeys="expandedKeys"
:selectedKeys="selectedKeys"
:treeData="treeData"
@expand="onExpand"
@select="onSelect"
>
<a-icon slot="folder" class="folder" type="folder" />
<a-icon slot="image" class="image" type="file-image" />
<a-icon slot="audio" class="audio" type="audio" />
<a-icon slot="video" class="video" type="video-camera" />
<a-icon slot="docx" class="docx" type="file-word" />
<a-icon slot="xml" class="xml" type="file-excel" />
<a-icon slot="pdf" class="pdf" type="file-pdf" />
<a-icon slot="zip" class="zip" type="file-zip" />
<a-icon slot="txt" class="txt" type="file-text" />
</a-tree>其中樣式如下,可以自己任意修改:
#ziptree .anticon{
font-size: 20px; color: #08c
}
#ziptree .anticon.folder{
color: #e7c146
}
#ziptree .anticon.docx{
color: #2a0ae2
}
#ziptree .anticon.pdf{
color: #e90b1e
}
#ziptree .anticon.xml{
color: #047449
}
#ziptree .anticon.zip{
color: #435892
}
#ziptree .anticon.txt{
color: #b9c6e7
}
#ziptree .anticon.image{
color: #82c0a8
}4. 通過(guò)接口獲取數(shù)據(jù)并處理數(shù)據(jù):
downloadAttachmentStream(option).then((res)=>{
if (!res && res.status!=200 && res.data.type == "application/json") {
this.$message.error('找不到該文件')
return
}
let jszip = new JSZip()
jszip.loadAsync(res.data).then(zip=>{
let myData=[]
that.transformData(zip, myData,0,)
that.treeData=myData
that.$nextTick(()=>{
that.expandedKeys=['0']
})
})
});其中transformData方法如下:
transformData(obj, myData, level = 0) {
let id=0
if(Object.keys(obj.files).length==0){
let fname=this.fileName.substring(0, this.fileName.lastIndexOf("."))
let rootData={id:'0',parentId:'', key:'0',title:fname, fullName:fname+'/' ,type:'dir', children:[],slots:{ icon: "folder" }}
myData.push(rootData)
}else{
for (let key in obj.files) {
let array=key.split('/').filter(item => item != '')
if(array.length == level+1){
if (obj.files[key].dir) {
if(level==0){ // 根 只有一個(gè)
let rootData={id:'0',parentId:'', key:'0',title:array[level], fullName:key ,type:'dir', children:[],slots:{ icon: "folder" }}
myData.push(rootData)
this.transformData(obj, rootData,level+1)
}else{
// 非根目錄
if(key.indexOf(myData.fullName)===0 && key!= myData.fullName && array.length == level+1){
let newData={id:myData.id+'-'+id, key:myData.id+'-'+id,parentId:myData.id, title:array[level], type:'dir', children:[],fullName:key,slots:{ icon: "folder" }}
myData.children.push(newData)
id++
this.transformData(obj, newData,level+1)
}
}
}else{ // 文件
if(key.indexOf(myData.fullName)==0 && key!=myData.fullName){
let data= {id:myData.id+'-'+id,parentId:myData.id, key:myData.id+'-'+id,title:array[level], type:array[level].replace(/.+\./, ""),fullName:key,}
if(['jpg','png','gif'].includes(data.type)){
data.slots={icon: "image"}
}else if(data.type=='mp3'){
data.slots={icon: 'audio'}
}else if(data.type=='mp4'){
data.slots={icon: 'video'}
} else if(data.type=='xlsx'){
data.slots={icon: 'xml'}
} else{
data.slots={icon: data.type}
}
myData.children.push(data)
id++
}
}
}else{
//
}
}
}
return myData;
},經(jīng)過(guò)以上步驟就能實(shí)現(xiàn)一個(gè)很漂亮的壓縮包的目錄樹(shù)了。
到此這篇關(guān)于vue中預(yù)覽zip的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)vue預(yù)覽zip內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue指令?v-bind的使用和注意需要注意的點(diǎn)
這篇文章主要給大家分享了?v-bind的使用和注意需要注意的點(diǎn),下面文章圍繞?v-bind指令的相關(guān)資料展開(kāi)內(nèi)容且附上詳細(xì)代碼?需要的小伙伴可以參考一下,希望對(duì)大家有所幫助2021-11-11
Vue3獲取響應(yīng)式數(shù)據(jù)的四種方法
Vue 3 引入了一個(gè)全新的響應(yīng)式系統(tǒng),其中最核心的就是 reactive 和 ref,它們是實(shí)現(xiàn)響應(yīng)式數(shù)據(jù)的基礎(chǔ),用于創(chuàng)建可以自動(dòng)跟蹤變化并更新視圖的對(duì)象和變量,本文介紹了Vue3獲取響應(yīng)式數(shù)據(jù)的四種方法,需要的朋友可以參考下2024-08-08
vue-cli3.X快速創(chuàng)建項(xiàng)目的方法步驟
這篇文章主要介紹了vue-cli3.X快速創(chuàng)建項(xiàng)目的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
vue實(shí)現(xiàn)兩列水平時(shí)間軸的示例代碼
本文主要介紹了vue實(shí)現(xiàn)兩列水平時(shí)間軸的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Vue3+TypeScript實(shí)現(xiàn)Docx/Excel預(yù)覽組件
這篇文章主要為大家詳細(xì)介紹了如何使用Vue3+TypeScript實(shí)現(xiàn)Docx/Excel預(yù)覽組件,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考下2024-04-04
如何構(gòu)建 vue-ssr 項(xiàng)目的方法步驟
這篇文章主要介紹了如何構(gòu)建 vue-ssr 項(xiàng)目的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
vue項(xiàng)目中如何將當(dāng)前頁(yè)面生成圖片
這篇文章主要介紹了vue項(xiàng)目中如何將當(dāng)前頁(yè)面生成圖片問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10

