<template>
<div>
<el-upload action="fakeaction" ref="upload"
:disabled="disabled"
:file-list="fileList"
:on-change="fileChange"
:before-upload="beforeUpload"
:http-request="submitUpload"
:on-success="success"
:drag="drag"
:multiple='multiple'
:limit="limit">
<div class="my_temp" v-if="drag">
<slot>
<i class="el-icon-upload"></i>
<div class="el-upload__text">{{title_name}}</div>
</slot>
</div>
<el-button v-else slot="trigger" size="small" type="primary" :disabled="disabled">{{title_name}}</el-button>
<div style="display:none" slot="file"></div>
</el-upload>
<div class="file_wrap">
<div class="file_item" v-for="file of fileWrap" :key="file.uid">
<i class="el-icon-document"></i>
<span>{{file.name}}</span>
<i class="el-icon-close" @click="handleRemove(file)"></i>
</div>
</div>
</div>
</template>
<script>
/**
* date 2021/8/18
* author @ddy
* @Description 文件上傳組件
* @param {Array} propFile 選 文件list [{name:'',uid:'123'}]
* @param {Array} fileType 選 文件類型 ['']
* @param {String} url 選 接口調(diào)用地址
* @param {String} title_name 按鈕名稱
* @param {Boolean} multiple 選 是多文件上傳
* @param {Boolean} drag 選 是否支持拖拽上傳
* @param {Boolean} disabled 選 禁用上傳
* @param {Number} limit 選 文件上傳數(shù)量最大限制
* @param {Number} size 選 文件上傳大小最大限制
* @param {Function} success 選 上傳成功方法
* @param {Function} fileChange 選 文件上傳成功或者移除后的方法 可以理解為覆蓋默認(rèn)文件改變的方法
* @use <upload-file :propFile="[{name:'',uid:'123'}]" url="api/dd" :limit="1" :disabled="fileDisabled" @fileChange="submitUpload" @success="success"/>
*/
import { UploadFile } from "@/utils/require"
export default {
name:'UploadFile',
props:{
propFile:{
type: Array,
default: () => []
},
multiple:{
type: Boolean,
default: false
},
drag:{
type: Boolean,
default: false
},
limit:{
type: Number,
default: 1
},
disabled:{
type: Boolean,
default: false
},
url:{
type: String,
default: ''
},
title_name:{
type: String,
default: '選擇附件'
},
fileType:{
type: Array,
default:()=>['pdf','doc','docx']
},
size:{
type: Number,
default: 20
}
},
watch:{
propFile:{
handler:function(val,old){
this.$nextTick(()=>{
this.fileWrap=val||[];
this.fileList=val||[];
})
},
immediate:true,
deep:true
}
},
data() {
return {
fileWrap:[],
fileList:[]
}
},
methods:{
beforeUpload(file){
let name=file.name.substring(file.name.lastIndexOf('.')+1);
const isJPG =!this.fileType.length||this.fileType.indexOf(name)>-1;
const isLt2M = file.size / 1024 / 1024 < this.size;
if (!isJPG) {
let str=this.fileType.map(item=> '.'+item).join(" ")
this.$message.error('上傳文件只能是 '+str+'格式 !')
this.handleRemove(file)
return false
}
if (!isLt2M) {
this.$message.error('上傳文件大小不能超過 '+this.size+'MB!')
this.handleRemove(file)
return false
}
},
fileChange(file, fileList){//上傳文件時(shí)限制格式
},
handleRemove(file) {//刪除文件
//無論是新上傳的還是編輯回顯的文件,file的uid都是他的唯一標(biāo)識id,但是傳入回顯數(shù)據(jù)時(shí)要對數(shù)據(jù)綁定賦值uid字段,要不然這邊f(xié)ile找不到
this.fileList=this.fileList.filter(item=>item['response']&&item['response']['annex_id']!=file['uid']);
this.getImg();
},
success(response, file, fileList){
this.fileList=fileList;
this.getImg();
},
submitUpload(file) {//上傳到服務(wù)器
let params = new FormData()
params.append('file',file.file)
UploadFile(params,{url:this.url}).then(res => {
if(res.errno===0&&!!res.data){
this.$message.success('上傳成功')
this.$emit('success',res.data);
file.onSuccess(res.data[0])
}else{
this.$message.error(res.errmsg)
}
}).catch(err => {
this.$message.error(err.errmsg)
this.handleRemove(file)
})
},
getImg(){
let arr = JSON.parse(JSON.stringify(this.fileList))
arr.map((item,i)=>{
if(item.response){
item.uid = item.response.annex_id
}
})
this.$nextTick(()=>{
this.fileWrap=arr;
})
this.$emit('fileChange',arr);
}
}
}
</script>
<style scoped lang='scss'>
/deep/ .el-upload-dragger{
border: 2px dashed #d9d9d9;
position: relative;
.my_temp{
position: absolute;
width: 100%;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
.el-icon-upload{
margin-top: 0px;
}
}
}
.file_wrap{
.file_item{
display: flex;
align-items: center;
background: #F4F6FB;
padding:0 5px;
margin: 5px 0;
>span{
flex: 1;
color: #636B78;
margin: 0 10px;
}
i{
color: #C4C4C4;
font-size: 20px;
&:nth-of-type(2){
cursor: pointer;
}
}
}
}
.hide_add{
/deep/ .el-upload--picture-card {
display: none;
}
}
/deep/ .el-upload-list{
display: none;
}
</style>
vue上傳文件封裝
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
禁止轉(zhuǎn)載黔酥,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者藻三。
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來枚钓,“玉大人铅搓,你說我怎么就攤上這事∶卦耄” “怎么了狸吞?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我蹋偏,道長便斥,這世上最難降的妖魔是什么捧请? 我笑而不...
- 正文 為了忘掉前任囤锉,我火速辦了婚禮荠耽,結(jié)果婚禮上鸳劳,老公的妹妹穿的比我還像新娘毯辅。我一直安慰自己歧强,他們只是感情好窄驹,可當(dāng)我...
- 文/花漫 我一把揭開白布缘揪。 她就那樣靜靜地躺著脓斩,像睡著了一般木西。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上随静,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼昭卓!你這毒婦竟也來了愤钾?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬榮一對情侶失蹤葬凳,失蹤者是張志新(化名)和其女友劉穎绰垂,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體火焰,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡劲装,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了昌简。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片占业。...
- 正文 年R本政府宣布念恍,位于F島的核電站六剥,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏峰伙。R本人自食惡果不足惜疗疟,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望瞳氓。 院中可真熱鬧策彤,春花似錦、人聲如沸匣摘。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽音榜。三九已至庞瘸,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間赠叼,已是汗流浹背恕洲。 一陣腳步聲響...
- 正文 我出身青樓葛家,卻偏偏與公主長得像户辞,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子癞谒,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 查看minio官網(wǎng),看到minio可以應(yīng)用在不同的環(huán)境下,這里著重講一下minio的js版,并在vue中運(yùn)行,并且...
- 新到手一個(gè)文件上傳功能底燎,具體實(shí)現(xiàn)是,當(dāng)截圖之后弹砚,在文本框中或者type=file的文本框中双仍,粘貼的時(shí)候(ctrl+...
- antd-vue上傳文件upload組件使用自定義上傳方法customRequest無法顯示文件上傳進(jìn)度條,如下圖...
- 最近項(xiàng)目中涉及很多文件上傳的地方,然后文件上傳又有很多限制茅诱。比如文件大小限制逗物,文件個(gè)數(shù)限制,文件類型限制瑟俭,文件上傳...