vue上传图片

vite.config.ts配置

 server: {

      port: 4000,

      proxy: {

        // 选项写法

        '/auth': {

          target: 'http://10.20.10.18/',

          changeOrigin: true,

          // rewrite: path => path.replace(/^\/auth/, '')

        },

        '/api': {

          target: 'http://10.20.10.18/',

          changeOrigin: true,

          // rewrite: path => path.replace(/^\/api/, '')

        }

      },

      hmr: {

        overlay: false

      },

      host: '0.0.0.0'

    },

// 上传图片的接口定义

export const uploadImagApi = async (data) => {

  return request.post({

    url: '/api/uploads/images',

    // data: params,

    data,

    headersType: 'application/x-www-form-urlencoded multipart/form-data'

  })

}

// 图片上传的方法定义

const uploadFile: any = (param: UploadRequestOptions) => {

  let formData = new FormData()

  formData.append('file', param.file)

  common.uploadImagApi(formData).then(() => {

    ElMessage.success('上传成功')

  })

}

//elementplus的upload组件使用-上传头像场景

<el-upload :http-request="uploadFile" list-type="picture-card" :auto-upload="true">

              <div class="flex-col justify-center items-center text-[#a3adbf]">

                <div class="w-64px flex justify-center"

                  ><el-icon :size="40" color="#a3adbf"><Plus /></el-icon

                ></div>

                <div class="w-64px">上传照片</div>

              </div>

              <template #file="{ file }">

                <div>

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

                    >

                      <el-icon><zoom-in /></el-icon>

                    </span>

                    <span

                      v-if="!disabled"

                      class="el-upload-list__item-delete"

                      @click="handleDownload(file)"

                    >

                      <el-icon><Download /></el-icon>

                    </span>

                    <span

                      v-if="!disabled"

                      class="el-upload-list__item-delete"

                      @click="handleRemove(file)"

                    >

                      <el-icon><Delete /></el-icon>

                    </span>

                  </span>

                </div>

              </template>

            </el-upload>

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值