图片上传

上代码:

<template>
  <div>
    <el-upload
      class="avatar-uploader"
      action="http://kumanxuan1.f3322.net:8360/admin/upload/goodNewPic"
      :headers="headers"
      name="good_pic"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <p>--------------------------------------------------------</p>
      <p>--------------------------------------------------------</p>
      <el-upload
        class="avatar-uploader"
        action
        :show-file-list="false"
        :before-upload="beforeAvatarUpload"
        :http-request="httpRequest"
      >
        <img v-if="imageUrl1" :src="imageUrl1" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    
  </div>
</template>

<script> 
import { postUploadImg } from "@/request/api"
export default {
  data() {
    return {
      imageUrl1: "",
      imageUrl: "",
      headers: {
        "X-Nideshop-Token": localStorage.getItem("token"),
      },
    };
  },
  methods: {
    httpRequest(data){
    // 发送上传图片请求
    console.log(data.file);
    let form  = new FormData();
    form.append("good_pic", data.file);
    
    postUploadImg(form).then(res => {
        if(res.errno === 0){
            this.imageUrl1 = res.data.fileUrl;
        }
    })
    },
    handleAvatarSuccess(response, file, fileList) {
      console.log(response);
      //   console.log( file);
      //   console.log( fileList);
      //   this.imageUrl = fileList.fileUrl;
      if (response.errno === 0) {
        this.imageUrl = response.data.fileUrl;
      }
    },
    beforeAvatarUpload(file) {
      console.log(file);
    },
   
  },
};
</script>
 
<style lang = "less" scoped>
</style>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容