通过vue3点击事件来获取剪切板中的图片!

        由于 navigator.clipboard 方法受安全协议影响,只能在https、127.0.0.1或localhost等域名中使用,无法在http中来实现,为此找到了第二种方法;

        一种是使用了navigator.clipboard ,另一种是通过input;

第一种:

首先是html部分

<template>
  <div style="width: 100px;height: 100px;border: 1px solid #000;text-align: center;line-height: 100px;margin: 50px auto;">
    <img v-if="pastedImage" :src="pastedImage" style="width: 100%;height: 100%;" />
    <el-icon v-else class="avatar-uploader-icon" @click="add"><Plus /></el-icon>
  </div>
</template>

js部分

<script setup>
import { ref } from "vue";
import { Plus } from "@element-plus/icons-vue";
const pastedImage = ref("");
async function add() {
  try {
    const clipboardItems = await navigator.clipboard.read();
    for (const clipboardItem of clipboardItems) {
      for (const type of clipboardItem.types) {
        if (type === "image/png") {
          const blob = await clipboardItem.getType(type);
          pastedImage.value = URL.createObjectURL(blob);
          // console.log(pastedImage.value);
          return;
        }
      }
    }
    console.error("剪贴板中没有图片");
  } catch (error) {
    console.error("获取剪贴板图片失败:", error);
  }
}
</script>

第二种:// 此方法必须用户手动粘贴

首先html部分

<template>
  <div>
    <input id="pasteInput" @paste="handlePaste" placeholder="上传图片" />
    <div style="display: flex; margin-left: 15px">
      <div style="display: flex; flex-wrap: wrap">
        <div class="imgBox" v-for="(item, index) in list" :key="index">
          <img
            @click="adds(item.picSrc)"
            :src="item.picSrc"
            style="width: 100px; height: 100px; border-radius: 10px"
          />
          <div @click="del(index)" v-if="list.length" class="delgb">X</div>
        </div>
      </div>
      <div v-if="pastedImage" class="imgSrc">
        <img
          :src="pastedImage"
          alt=""
          style="width: 80%; height: 80%; margin-top: 80px"
        />
        <p @click="kong" class="imgSrcgb">X</p>
      </div>
    </div>
  </div>

js部分

const handlePaste = async (event) => {
  const items = (event.clipboardData || event.originalEvent.clipboardData)
    .items;
  for (const item of items) {
    if (item.type.indexOf("image") === 0) {
      const blob = item.getAsFile();
      if (blob) {
        // pastedImage.value = URL.createObjectURL(blob);
        // list.value.push(URL.createObjectURL(blob));
        const file = blob;
        const reader = new FileReader();
        reader.onload = (e) => {
          const imageData = e.target.result;
          let formData = new FormData();
          formData.append("file", blob);
          postUploadFile(formData).then((res) => {
            list.value.push({
              id: row_id.value,
              picSrc: res?.data.imgSrc,
              picId: res.data.fileid,
            });
            row_yjtpid.value = res.data.fileid;
            row_yjtpsrc.value = res.data.imgSrc;
            let id = list.value[0].id;
            let picSrc = "";
            let picId = "";
            for (let i = 0; i < list.value.length; i++) {
              picSrc += list.value[i].picSrc;
              picId += list.value[i].picId;
              if (i !== list.value.length - 1) {
                picSrc += ",";
                picId += ",";
              }
            }
            transformedData.value = { id: id, picSrc: picSrc, picId: picId };
            // console.log(transformedData);
            postSavePic(transformedData.value).then((res) => {
              // console.log(res);
            });
          });
        };
        reader.readAsDataURL(file);
        return;
        // break;
      }
    }else {
      ElMessage({
        message: '请先截图!',
        type: 'warning',
      })
      setTimeout(() =>{
        document.getElementById('pasteInput').value = ''
      })
      return;
      }
  }
};

const del = (e) => {
  list.value.splice(e, 1);
  // add();
  if (list.value.length) {
    let id = list.value[0].id;
    let picSrc = "";
    let picId = "";
    for (let i = 0; i < list.value.length; i++) {
      picSrc += list.value[i].picSrc;
      picId += list.value[i].picId;
      if (i !== list.value.length - 1) {
        picSrc += ",";
        picId += ",";
      }
    }
    transformedData.value = { id: id, picSrc: picSrc, picId: picId };
  } else {
    transformedData.value = { id: row_id.value, picSrc: "", picId: "" };
  }
  // console.log(transformedData);
  postSavePic(transformedData.value).then((res) => {
    // console.log(res);
  });
};
const adds = (e) => {
  pastedImage.value = e;
};
const kong = () => {
  pastedImage.value = "";
};

 css样式

<style lang="scss" scoped>
#pasteInput {
  color: #fff;
  width: 200px;
  height: 50px;
  outline: none;
  font-size: 20px;
  font-weight: 700;
  text-align: center;
  border-radius: 15px;
  border: 0 solid #000;
  background-color: blue;
  caret-color: rgba(255, 255, 255, .1);
}
input::-webkit-input-placeholder {
  color: #fff;
}
</style> 

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值