前端上传图片

headers要改为form-data

headers: {
 'Content-Type': 'multipart/form-data',
},
<template>
  <div>

    <el-upload action="#"
               list-type="picture-card"
               :auto-upload="false"
               :on-change="handleAvatarSuccess">
      <i slot="default"
         class="el-icon-plus"></i>
      <div slot="file"
           slot-scope="{file}">
        <img class="el-upload-list__item-thumbnail"
             :src="file.url"
             alt="">
        <span class="el-upload-list__item-actions">
          <span class="el-upload-list__item-preview"
                @click="handlePictureCardPreview(file)">
            <i class="el-icon-zoom-in"></i>
          </span>
          <span v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleDownload(file)">
            <i class="el-icon-download"></i>
          </span>
          <span v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleRemove(file)">
            <i class="el-icon-delete"></i>
          </span>
        </span>
      </div>
    </el-upload>

    <el-dialog :visible.sync="dialogVisible">
      <img width="100%"
           :src="dialogImageUrl"
           alt="">
    </el-dialog>

    <button @click="uploadPicture">上传图片</button>

  </div>

</template>

<script>




import { materialPictureUpload, materialPictureGetlist } from '@/api/goods/goods' // 获取接口
export default {
  name: 'testabc',

  components: {

  },
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      file: null
    };
  },
  methods: {

    handleRemove(file) {
      console.log(file);
    },
    handlePictureCardPreview(file) {
      console.log(file)
      this.dialogImageUrl = file.url;

      this.dialogVisible = true;
    },
    handleDownload(file) {

    },

    handleAvatarSuccess(file, fileList) {
      console.log(file);
      this.file = file.raw
      console.log(this.file);

      let obj = {
        id: '1',
        creator: 'material',
        uploadtime: '2023-10-30 14:00:00',
        filename: 'logo.jpg',
        type: 'material',
      }
      const formData = new FormData()
      formData.append('jsondata', JSON.stringify(obj))
      formData.append('file', this.file)

      console.log(formData)

    },

    // 点击上传图片
    uploadPicture() {
      materialPictureUpload(formData).then(res => {
        console.log(res)
      })
    }


  },
  mounted() {

    let obj = {

    }
    materialPictureGetlist(obj).then(res => {
      console.log(res)
    })

  }
}
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值