<template>
<div >
<el-input
@paste.native="pasteImg($event)"
v-model="value"
>
</el-input>
</div>
</template>
<script type="text/javascript">
export default {
mounted() {
},
data() {
return {
value: "",
canUpload: true,
fileList: []
}
},
methods: {
pasteImg(event){
//粘贴上传图片
let txt = event.clipboardData.getData('Text')
if(typeof(txt) == 'string' && txt !== '') {
//检测到是文字,过滤
return ;
}
let file = null
const items = (event.clipboardData || window.clipboardData).items
if(items.length) {
for (let i = 0; i < items.length; i++) {
if (items[i].type.indexOf('image') !== -1) {
file = items[i].getAsFile()
this.handleChange(file)
if (!this.canUpload) {
this.canUpload = !this.canUpload;
}
break
}
}
}
},
// 图片上传
handleChange(file) {
let formData = new FormData()
formData.append('file', file.raw || file)
this.postUrl('/www/upload/upload_img', formData).then((response) => {
this.fileList.push(response.data)
console.log(this.fileList)
}, (error) => {
console.log(error)
});
}
}
}
</script>
评论区可直接测试:
https://bbs.nanshengbbs.top/detail/357
还得是大佬
佬
原来是这样的吗
666