Vue.js中复制图片在输入框粘贴 Ctrl+V实现上传

<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>

你可能感兴趣的:(Vue.js,H5,技术开发,vue)