vue_upload上传图片支持多选、拖拽上传
page.png
1.直接在要使用的页面中添加这代码即可:
<template>
<div class="hello" >
<div class="upload">
<div class="upload_warp_left" @click="fileClick" >
<button class="btn btn-success">上传图片</button>
</div>
<div class="upload_warp_text">
选中{{imgList.length}}张文件,共{{bytesToSize(this.size)}}; 最多上传<span class="text-primary"> 6</span> 张图片!
</div>
<div class="upload_warp" style="border: 1px solid white;">
<div class="upload_warp_img" v-show="imgList.length!=0" >
<div class="upload_warp_img_div" v-for="(item,index) of imgList" :key="item.id">
<div class="upload_warp_img_div_top" >
<div class="upload_warp_img_div_text" >
{{item.file.name}}
</div>
<img src="" class="upload_warp_img_div_del" @click="fileDel(index)">
</div>
<img :src="item.file.src">
</div>
</div>
</div>
<input @change="fileChange($event)" type="file" id="upload_file" multiple style="display: none"/>
</div>
</div>
</template>
<script>
export default {
data () {
return {
imgList: [],
size: 0
}
},
methods: {
fileClick(){
document.getElementById('upload_file').click()
},
fileChange(el){
if (!el.target.files[0].size) return;
this.fileList(el.target.files);
el.target.value = ''
},
fileList(files){
for (let i = 0; i < files.length; i++) {
this.fileAdd(files[i]);
}
},
fileAdd(file){
this.size = this.size + file.size;//总大小
let reader = new FileReader();
reader.vue = this;
// reader.readAsDataURL(file);
reader.onload = function () {
file.src = this.result;
this.vue.imgList.push({
file
});
}
console.log("imgList",this.imgList)
},
fileDel(index){
this.size = this.size - this.imgList[index].file.size;//总大小
this.imgList.splice(index, 1);
},
bytesToSize(bytes){
if (bytes === 0) return '0 B';
let k = 1000, // or 1024
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
i = Math.floor(Math.log(bytes) / Math.log(k));
return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
},
dragenter(el){
el.stopPropagation();
el.preventDefault();
},
dragover(el){
el.stopPropagation();
el.preventDefault();
},
drop(el){
el.stopPropagation();
el.preventDefault();
this.fileList(el.dataTransfer.files);
},
//在确定上传调用函数发送ajax请求
compile(){
let _this=this;
let url=‘’
var data=new FormData();
for(var i=0;i<this.imgList.length;i++){
//遍历图片
data.append("filearr",this.imgList[i].file);
}
data.append("_token", this.tokens);
this.$axios.post(url,data).then(function(rest){
}).catch(function(er){
})
},
}
}
</script>
<style lang="">
.upload_warp_img_div_del {
position: absolute;
top: 6px;
width: 16px;
right: 4px;
}
.upload_warp_img_div_top {
position: absolute;
top: 0;
width: 100%;
height: 30px;
background-color: rgba(0, 0, 0, 0.4);
line-height: 30px;
text-align: left;
color: #fff;
font-size: 12px;
text-indent: 4px;
}
.upload_warp_img_div_text {
white-space: nowrap;
width: 80%;
overflow: hidden;
text-overflow: ellipsis;
}
.upload_warp_img_div img {
max-width: 100%;
max-height: 100%;
vertical-align: middle;
}
.upload_warp_img_div {
position: relative;
height: 100px;
width: 120px;
border: 1px solid #ccc;
margin: 0px 5px 5px 0px;
float: left;
line-height: 100px;
display: table-cell;
text-align: center;
background-color: #eee;
cursor: pointer;
}
.upload_warp_img {
border-top: 1px solid #D2D2D2;
padding: 5px 0 0 5px;
overflow: hidden
}
.upload_warp_text {
text-align: left;
margin-bottom: 5px;
padding-top: 5px;
text-indent: 14px;
border-top: 1px solid #ccc;
font-size: 14px;
}
.upload_warp_right {
float: left;
width: 57%;
margin-left: 2%;
height: 100%;
border: 1px dashed #999;
border-radius: 4px;
line-height: 130px;
color: #999;
}
.upload_warp_left button {
margin: 8px 5px 0px 5px;
cursor:pointer;
}
.upload_warp_left {
float: left;
}
.upload_warp {
margin: 5px;
}
.upload {
border-left: 1px solid #ccc;
border-right: 1px solid #ccc;
background-color: #fff;
width: 770px;
box-shadow: 0px 1px 0px #ccc;
border-radius: 4px;
}
.hello {
width: 770px;
text-align: center;
}
</style>
网友评论