element-ui实现图片上传,以图搜图接口对接

今天做项目的时候,拿到了一个接口文档,如下:
在这里插入图片描述
哎,一开始就不会上传图片怎么办呢,想办法干啊!最后整理出来了上传图片的方法,用element-uid upload方法,直接上代码;

<div class="image_search">
          <el-upload
            :headers="headerObj"
            :before-upload="handleBeforeUpload"
            list-type="picture"
            show-file-list="false"
          >
            <el-button title="Search by image " type="default" icon="el-icon-camera"></el-button>
          </el-upload>
        </div>

data区域:

headerObj: {},

computed区域:

   handleBeforeUpload(file) {
      console.log(file);
      this.searchInput = file.name;
      this.file = file;
      this.$router.push({
        name: "ImgSearchResults",
        params: {
          file: this.file,
        },
      });
    }

请求成功跳转到新页面:

<template>
  <div  element-loading-text="please waiting..."
   v-loading="loading">
    <div class="images">
      <div  v-for="(item, index) in images" :key="index" class="img">
        <img :src="`http://localhost:8080/wes/${item.path}`" alt="" />
      </div>
    </div>
    <div class="page">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="pageChange"
        @prev-click="pageChange"
        @next-click="pageChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "ImgSearchResults",
  data() {
    return {
      loading: false,
      images: [],
      file: null,
      pn: 0,
      rn: 12,
      total: 120,
    };
  },
  created() {
    this.init();
    this.query();
  },
  methods: {
    init() {
      this.file = this.$route.params.file;
    },
    pageChange(page) {
      this.pn = this.rn * (page - 1);
      this.query();
      if(this.pn===100){
        this.$message.console.error("已经到底啦");
      }
    },
    async query() {
      let fileData = new FormData();
      fileData.append("file", this.file);
      fileData.append("pn", this.pn);
      fileData.append("rn", this.rn);
      this.loading = true;
      const { data: res } = await this.$http.post("picture/search", fileData);
      this.loading = false;
      this.images = res.data.briefs;
    },
  },
};
</script>

<style scoped lang="less">
.images {
  width: 1200px;
  margin: auto;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  .img {
    width: 280px;
    height: 210px;
    margin-bottom: 10px;;
    padding-top: 20px;
    img {
      width: 100%;
      height: 100%;
    }
  }
}
.page {
  text-align: center;
  margin-top: 20px;
}
</style>

最终效果如下:
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
最后就是成功啦!!代码片段大家按需取要,可以私信我讨论交流,注意是formdata传值哈!!祝大家code happy

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞翔的鱼。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值