欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue項(xiàng)目el-upload?上傳文件及回顯照片和下載文件功能實(shí)現(xiàn)

 更新時(shí)間:2023年12月21日 11:55:33   作者:Aohan-z  
本次需求是上傳多種固定格式的文件,且回顯的時(shí)候,圖片可以正常顯示,文件可以進(jìn)行下載,主要采用element的el-upload組件實(shí)現(xiàn),對(duì)Vue項(xiàng)目el-upload?上傳文件及回顯照片和下載文件功能實(shí)現(xiàn)感興趣的朋友跟隨小編一起看看吧

本次需求是上傳多種固定格式的文件,且回顯的時(shí)候,圖片可以正常顯示,文件可以進(jìn)行下載
主要采用element的el-upload組件實(shí)現(xiàn)

1、文件上傳

先看下,上傳文件效果圖
點(diǎn)擊上傳將文件放到網(wǎng)頁(yè),還有一個(gè)點(diǎn)擊確定的按鈕再上傳文件到服務(wù)器

在這里插入圖片描述

html

<el-upload
   ref="upload"
   accept=".png,.jpg,.jpeg,.doc,.docx,.txt,.xls,.xlsx"
   action="#"
   multiple
   :limit="5"
   :headers="headers"
   :auto-upload="false"
   :file-list="fileList"
   :on-change="handleChange"
   :on-remove="removeFile"
   :on-exceed="limitCheck"
 >
   <el-button size="small" type="primary">點(diǎn)擊上傳</el-button>
   <div slot="tip" class="el-upload__tip">
     <p>只支持.png / .jpg / .jpeg / .doc / .docx / .txt / .xls / .xlsx文件</p>
     <p>最多上傳5個(gè)文件</p>
   </div>
 </el-upload>

1、accept:限制上傳的文件類(lèi)型
2、action: 必選參數(shù),上傳的地址,可以暫時(shí)設(shè)置為"#"
3、multiple: 設(shè)置選擇文件時(shí)可以一次進(jìn)行多選
4、limit:限制上傳的文件的數(shù)量
5、auto-upload:是否自動(dòng)上傳,false為手動(dòng)上傳
(因?yàn)槲倚枰捅韱我黄鹛砑拥椒?wù)器,所以點(diǎn)擊上傳時(shí)只是到頁(yè)面,后面點(diǎn)擊確定才到服務(wù)器)
需要注意:當(dāng):auto-upload="false"手動(dòng)上傳的時(shí)候,:before-upload="beforeUpload"上傳前校驗(yàn)失效,兩者不可以同時(shí)用,可以將校驗(yàn)加在:on-change里面
6、file-list:文件列表

script
記得引入

import axios from 'axios'
data() {
	return {
		// 上傳附件
      fileList: [],
      headers: {
        'Content-Type': 'multipart/form-data'
      },
	}
},
methods: {
	// 文件狀態(tài)改變時(shí)的鉤子
	handleChange(file, fileList) { // 文件數(shù)量改變
      this.fileList = fileList
      const isLt2M = (file.size / 1024 / 1024 < 2)
      if (!isLt2M) {
        this.$message.error('上傳頭像圖片大小不能超過(guò) 2MB!')
        this.fileList.pop()
      }
      return isLt2M
    },
    // 文件超出個(gè)數(shù)限制時(shí)的鉤子
    limitCheck() {
      this.$message.warning('每次上傳限制最多五個(gè)文件')
    },
    // 文件刪除的鉤子
    removeFile(file, fileList) {
      this.fileList = fileList
    },
    // 點(diǎn)擊確定按鈕 上傳文件
    confirm() {
    	var param = new FormData()
    	this.fileList.forEach((val, index) => {
    		param.append('file', val.raw)
    	})
    	// 拿取其他的信息
    	param.append('id', sessionStorage.getItem('id'))
    	...
    	axios(`url......`, {
   			headers: {
              'Authorization': 'Bearer ' + sessionStorage.getItem('token'),
              'Content-Type': 'multipart/form-data'
            },
            method: 'post',
            data: param
          	}).then((res) => {
	            if (res.data.code === 200) {
	              this.$message.success('上傳成功')
	            } else {
	              this.$message.error('上傳失敗')
	            }
          })
    }
}

2、文件回顯

文件回顯時(shí),只展示上傳的內(nèi)容
如果上傳圖片,直接展示縮略圖,可進(jìn)行放大預(yù)覽操作
如果上傳其他文件,展示固定的圖片(圖片可自己設(shè)置),可在網(wǎng)頁(yè)進(jìn)行下載操作

在這里插入圖片描述


在這里插入圖片描述

html

<el-upload
   :file-list="fileList"
   action="#"
   list-type="picture-card"
 >
   <div slot="file" slot-scope="{file}">
     <img
       class="el-upload-list__item-thumbnail"
       :src="file.url"
       alt=""
     >
     <span class="el-upload-list__item-actions">
     	// 下載
       <span
         v-if="updataIf(file)"
         class="el-upload-list__item-delete"
         @click="handleDownload(file)"
       >
         <i class="el-icon-download" />
       </span>
       // 放大預(yù)覽
       <span
         v-else
         class="el-upload-list__item-preview"
         @click="handlePictureCardPreview(file)"
       >
         <i class="el-icon-zoom-in" />
       </span>
     </span>
   </div>
 </el-upload>
 <el-dialog :visible.sync="dialogVisible" :append-to-body="true">
   <img width="100%" height="100%" :src="dialogImageUrl" alt="">
 </el-dialog>

script
記得引入

import axios from 'axios'
data() {
	return {
		dialogImageUrl: '',
	    dialogVisible: false,
	    fileList: []
	}
},
created() {
	this.getDetail()
},
methods: {
	// 判斷文件類(lèi)型,圖片預(yù)覽,文件下載
    updataIf(e) {
      if (e.fileName) {
        if (e.fileName.split('.')[1] === 'png' || e.fileName.split('.')[1] === 'jpeg' || e.fileName.split('.')[1] === 'jpg') {
          return false
        } else {
          return true
        }
      } else {
        if (e.name.split('.')[1] === 'png' || e.name.split('.')[1] === 'jpeg' || e.name.split('.')[1] === 'jpg') {
          return false
        } else {
          return true
        }
      }
    },
	// 獲取詳情
	getDetail() {
		this.fileList = []
		// 調(diào)用查詢(xún)的接口
		...
		...
		// 調(diào)用成功
		if (res.code === 200) {
			var arr = []
			// res.data.fileMap為返回的Object格式的文件數(shù)據(jù)
			// 因?yàn)榉祷氐母袷绞?{name: url,name: url}
			// 循環(huán)轉(zhuǎn)變成需要的數(shù)組格式,每個(gè)數(shù)組元素包含name、url、id
			// [{name: name,url: ulr,id: id},{name: name,url: ulr,id: id}]
			Object.keys(res.data.fileMap).forEach(key => {
			   var obj = {
			     name: '',
			     url: '',
			     id: ''
			   }
			   // 數(shù)組元素里面的name就是key
			   obj.name = key
			   // 數(shù)組元素里面的url,當(dāng)時(shí)圖片的時(shí)候直接用url
			   // 當(dāng)不為圖片的時(shí)候,顯示固定的圖片,且傳入id(id用于下載文件)
			   if (key.split('.')[1] === 'png' || key.split('.')[1] === 'jpeg' || key.split('.')[1] === 'jpg') {
			     obj.url = 'http://.../file/minio/view/chs/' + res.data.fileMap[key]
			   } else {
			     // 所有文件統(tǒng)一圖片
			     obj.url = require('../../../assets/images/fileImg.png')
			     obj.id = res.data.fileMap[key]
			   }
			   arr.push(obj)
			 })
			 // 將組好的格式直接賦值給fileList(文件列表)
			 this.fileList = arr
		} 
	},
	// 放大預(yù)覽
	handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    // 文件下載
    handleDownload(file) {
      axios(`url.......`, {
        headers: {
          'Authorization': 'Bearer ' + sessionStorage.getItem('token'),
          'Content-Type': 'application/octet-stream'
        },
        methods: 'get',
        params: {
          id: file.id
        },
        responseType: 'blob'
      }).then((res) => {
        if (res.status === 200) {
          const content = res.data
          const blob = new Blob([content])
          const fileName = file.name
          if ('download' in document.createElement('a')) { // 非IE下載
            const elink = document.createElement('a')// 創(chuàng)建一個(gè)a標(biāo)簽通過(guò)a標(biāo)簽的點(diǎn)擊事件區(qū)下載文件
            elink.download = fileName
            elink.style.display = 'none'
            elink.href = URL.createObjectURL(blob)// 使用blob創(chuàng)建一個(gè)指向類(lèi)型數(shù)組的URL
            document.body.appendChild(elink)
            elink.click()
            URL.revokeObjectURL(elink.href) // 釋放URL 對(duì)象
            document.body.removeChild(elink)
          } else { // IE10+下載
            navigator.msSaveBlob(blob, fileName)
          }
        }
      }).catch(res => {
        console.log(res)
      })
    },
}

關(guān)于type: “application/octet-stream“ 格式的數(shù)據(jù)并下載主要參考下面這篇文章 http://www.dbjr.com.cn/javascript/3086322zm.htm

style
隱藏el-upload上傳

// 隱藏上傳
::v-deep .el-upload.el-upload--picture-card{
  display: none;
}
// 隱藏右上角綠色標(biāo)志
::v-deep .el-upload-list__item-status-label{
  display: none !important;
}

到此這篇關(guān)于Vue項(xiàng)目el-upload 上傳文件及回顯照片和下載文件功能實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue el-upload 上傳文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue分頁(yè)插件的使用方法

    vue分頁(yè)插件的使用方法

    這篇文章主要介紹了vue分頁(yè)插件的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • 詳解Vue3中ref和reactive函數(shù)的使用

    詳解Vue3中ref和reactive函數(shù)的使用

    這篇文章主要為大家詳細(xì)介紹了Vue3中ref和reactive函數(shù)的使用教程,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Vue有一定的幫助,需要的可以參考一下
    2022-07-07
  • vue里input根據(jù)value改變背景色的實(shí)例

    vue里input根據(jù)value改變背景色的實(shí)例

    今天小編就為大家分享一篇vue里input根據(jù)value改變背景色的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • vue使用element-ui的el-image的問(wèn)題分析

    vue使用element-ui的el-image的問(wèn)題分析

    這篇文章主要介紹了vue使用element-ui的el-image的問(wèn)題分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Vue頁(yè)面中實(shí)現(xiàn)平滑滾動(dòng)功能

    Vue頁(yè)面中實(shí)現(xiàn)平滑滾動(dòng)功能

    這是一個(gè)實(shí)現(xiàn)平滑滾動(dòng)的函數(shù),可以讓頁(yè)面在滾動(dòng)到指定位置時(shí)產(chǎn)生緩動(dòng)效果,本文給大家介紹了如何在在Vue頁(yè)面中實(shí)現(xiàn)平滑滾動(dòng)功能,<BR>,文中詳細(xì)的代碼講解供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下
    2023-12-12
  • 使用vue根據(jù)狀態(tài)添加列表數(shù)據(jù)和刪除列表數(shù)據(jù)的實(shí)例

    使用vue根據(jù)狀態(tài)添加列表數(shù)據(jù)和刪除列表數(shù)據(jù)的實(shí)例

    今天小編就為大家分享一篇使用vue根據(jù)狀態(tài)添加列表數(shù)據(jù)和刪除列表數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • vue實(shí)現(xiàn)百度搜索功能

    vue實(shí)現(xiàn)百度搜索功能

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)百度搜索功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • 使用Vue實(shí)現(xiàn)一個(gè)樹(shù)組件的示例

    使用Vue實(shí)現(xiàn)一個(gè)樹(shù)組件的示例

    這篇文章主要介紹了使用Vue實(shí)現(xiàn)一個(gè)樹(shù)組件的示例,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2020-11-11
  • vue中組件樣式?jīng)_突的問(wèn)題解決

    vue中組件樣式?jīng)_突的問(wèn)題解決

    默認(rèn)情況下,寫(xiě)在.vue組件中的樣式會(huì)全局生效,因此很容易造成組件之間的樣式?jīng)_突問(wèn)題,本文就來(lái)介紹一下如何解決此問(wèn)題,感興趣的可以了解一下
    2023-12-12
  • 在vue項(xiàng)目中集成graphql(vue-ApolloClient)

    在vue項(xiàng)目中集成graphql(vue-ApolloClient)

    這篇文章主要介紹了在vue項(xiàng)目中集成graphql(vue-ApolloClient),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09

最新評(píng)論