1. 程式人生 > 其它 >Element-UI : Upload 上傳檔案再編輯顯示的兩種方式

Element-UI : Upload 上傳檔案再編輯顯示的兩種方式

1.直接將檔名以文字的方式展現

後臺返回的連結以逗號的形式分隔,初始化定義一個數組:files: []
頁面結構:

<el-upload
    class="upload-demo"
    ref="upload"
    :action="actionPath"
    :file-list="files" >
  <el-button slot="trigger" size="small" type="primary">選取檔案</el-button
  </el-upload>

處理後臺返回的檔案連結程式碼:

let vm = this;
if
(res.attachment) { //後臺返回的檔案連結 let a = (res.attachment).split(','); if(a.length > 0) { a.forEach(item => { var obj = {} let index = item.lastIndexOf('\/'); let fileName = item.substring(index + 1, item.length); //最後的檔名截取出來 vm.$set(obj,'name',fileName); vm.$set(obj,
'url',item); //修改files裡面的結構(name,url) vm.files.push(obj); }) } }

最後的頁面顯示如下:

2.以圖示的方式展現
頁面結構及相關CSS:

<ul class="download-imgs">
    <li class="need-enclosure clearfix" v-for="(item, index) in attachment" :key="index">
        <img class="natural-img" :src="item"
v-if="(/.png|.jpg|.jpeg/).test(item)" style="position:relative" > <img :src="downLoadImg(item)" v-else style="position:relative"> <p>{{item | formatName}}</p> <div class="img-hover"> <div class="preview" v-if="(/.png|.jpg|.jpeg/).test(item)" @click="showImg(item)">預覽</div> <a class="preview" :href="item" target="_blank" v-else-if="(/.pdf|.txt/).test(item)">預覽</a> <a class="preview" :href="item" v-else>下載</a> </div> </li> </ul> <!--點選圖片放大dialog--> <el-dialog :visible.sync="dialogImg"> <img width="100%" :src="imgExpand" alt=""> </el-dialog>
.need-enclosure{
      display: inline-block;
      margin-right:5px;
      position: relative;
  }
  
.need-enclosure p { /* 檔名過長後顯示省略號 */
    width: 90px;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
}
  .preview {
      position: absolute;
      left:20px;
      bottom:20px;
      cursor: pointer;
      border: 1px solid #fff;
      padding: 0 10px;
      color: #fff;
  }
  .preview-img {
      width: 70px;
      height: 47px;

  }
  .natural-img{
      width: 90px;
      height: 67px;
  }
  .need-enclosure:hover .img-hover{
      display: block;
  }
  .img-hover {
      width: 90px;
      height: 67px;
      background: rgba(29,29,30,0.7);
      position: absolute;
      top: 0;
      left: 0;
      display: none;
  }

初始化資料:

downLoadImgs:[ 
     {type:0,url:'/static/images/bg_image.png'},
     {type:1,url:'/static/images/doc.png'},
     {type:2,url:'/static/images/ppt.png'},
     {type:3,url:'/static/images/excel.png'},
     {type:4,url:'/static/images/txt.png'},
     {type:5,url:'/static/images/pdf.png'},
     {type:6,url:'/static/images/zip.png'}
],

處理後臺返回檔案連結:

//返回顯示檔案圖示的地址函式
downLoadImg(item){
    if(/.txt/.test(item)){
        return this.downLoadImgs[4].url;
    }else if(/.doc|.docx/.test(item)){
        return this.downLoadImgs[1].url;
    }else if((/.png|.jpg|.jpeg/).test(item)){
        return this.downLoadImgs[0].url;
    }else if(/.ppt|pptx/.test(item)){
         return this.downLoadImgs[2].url;
     }else if(/.xls/.test(item)){
          return this.downLoadImgs[3].url;
     }else if(/.zip|.rar/.test(item)){
           return this.downLoadImgs[6].url;
      }else if(/.pdf/.test(item)){
            return this.downLoadImgs[5].url;
      }
},
            
//預覽執行結果的圖片
 showImg(url) {
     this.imgExpand = url;
    this.dialogImg = true;
 },

顯示檔名的過濾器:

filters: {
            formatName(cellvalue){
                //console.log(cellvalue)
                if(cellvalue.length > 9){
                    let index = cellvalue.lastIndexOf('\/');
                    let demandName = cellvalue.substring(index + 1,cellvalue.length)
                    return demandName;
                }else {
                    return cellvalue;
                }
            },
        }

最後的頁面顯示如下: