vue3 elelment-Plus el-update组件手动上传图片,不自动上传图片

一、组件库方式上传

<template>
<div class="front upload-item">
    <el-upload
    :limit="1"
    action="#"
    list-type="picture"
    :auto-upload="false"
    accept="image/png,image/gif,image/jpg,image/jpeg"
    :on-success="handleSuccess"
    :on-change="imgSaveToUrl"
    :show-file-list='false'
    ref='frontUpload'
    >
    <img  class="icon-img" v-if='imageFrontUrl' :src="imageFrontUrl"/>
    <img  class="icon-img" v-else src="@/assets/defaultImg.png"/>
</el-upload>
</template>

//js代码
const imageFrontUrl = ref('')
function imgSaveToUrl (file) {
    imageFrontUrl.value = URL.createObjectURL(file.raw)
    const fileSize = file.size
    const fileType = file.raw.type
    if (!fileSize) {
    // 是否为空
        ElMessage({
            type: 'error',
            showClose: true,
            message: '无效的文件,请重新选择!',
        })
        logoPicRemove()
        return
    }
    if (fileSize / 1024 / 1024 > 10) {
    // 图片大小
      ElMessage({
            type: 'error',
            showClose: true,
            message: '请上传小于10M的图片!',
        })
        logoPicRemove()
        return
    }
    if (fileType.indexOf('image') === -1) { 
    // 如果不是图片格式
        ElMessage({
            type: 'error',
            showClose: true,
            message: '不是有效的图片文件,或格式不支持,请重新选择!',
        })
        logoPicRemove()
        return
    }
        imageUpload(file)
    }

function logoPicRemove() { // 清空内容
    frontUpload.value.clearFiles()
    imageFrontUrl.value = ''
}
async function imageUpload (file) {
  let res = await axios.post({
   ........ 
  })
}

值得一提的是还有一种将raw文件类型转换成url的方式

// 获取选中图片的预览路径,并赋值给本地img路径,在前端展示
    imgSaveToUrl (file) {
      // 获取上传图片的本地URL,用于上传前的本地预览,转换后的地址为 blob:http://xxx/7bf54338-74bb-47b9-9a7f-7a7093c716b5
      this.imageUrl = URL.createObjectURL(file.raw)
      console.log('图片预览地址:', this.imageUrl)
    }
  }

二、原生上传图片方式

<template>
  <div  @click.capture.stop="handle_doUpload">
    <input
      type="file"
      accept="image/*"
      ref="getFile"
      @change="handle_choseFile"
    />
  </div>
</template>
const emit = defineEmits(['update:fileUrl'])
defineProps({
  fileUrl: {
    type: String,
    default: ""
  },
})

const getFile = ref(null)
function handle_doUpload() {
  getFile.value.click()
}
async function handle_choseFile(e) {
  let file = getFile.value.files[0]
  if (file.size > 10 * 1024 * 1024) {
    // 文件大小超限了
    ElMessage.closeAll()
    ElMessage({
      type: 'error',
      showClose: true,
      message: '请上传小于10M的图片'
    })
    getFile.value.value = '' // 清空内容
    return
  }
  let forms = new FormData()
  forms.append('file', file)
  forms.append('filePath', `pc/client-${moment().format('YYYY-MM-DD')}/`)
  getFile.value.value = '' // 清空内容
  let res = await axios.post({
   ........
  })
  if (res) {
    emit("update:fileUrl", res.data) // 其他操作
  }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用 Vue3 和 Element Plus 的 Drawer 组件来封装一个抽屉组件。首先需要在组件中引入 Drawer 组件,然后在模板中使用 Drawer 组件的属性和事件来实现抽屉的展示和隐藏。 以下是一个简单的抽屉组件的代码示例: ```vue <template> <el-drawer :visible="visible" :title="title" :size="size" :direction="direction" :before-close="beforeClose" @open="handleOpen" @close="handleClose" > <slot></slot> </el-drawer> </template> <script> import { ref } from 'vue'; import { ElDrawer } from 'element-plus'; export default { name: 'MyDrawer', components: { ElDrawer, }, props: { visible: { type: Boolean, default: false, }, title: { type: String, default: '', }, size: { type: String, default: '30%', }, direction: { type: String, default: 'rtl', }, }, setup(props, { emit }) { const beforeClose = (done) => { // 可以在这里添加关闭前的逻辑 done(); }; const handleOpen = () => { emit('update:visible', true); }; const handleClose = () => { emit('update:visible', false); }; return { beforeClose, handleOpen, handleClose, }; }, }; </script> ``` 在父组件中使用该抽屉组件时,可以通过 v-model 来控制抽屉的显示和隐藏: ```vue <template> <div> <el-button @click="visible = true">打开抽屉</el-button> <my-drawer v-model="visible" title="抽屉标题"> 抽屉内容 </my-drawer> </div> </template> <script> import MyDrawer from './MyDrawer.vue'; export default { components: { MyDrawer, }, data() { return { visible: false, }; }, }; </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

柑橘乌云_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值