VUE3+ElementPlus如何实现文件上传

VUE3+ElementPlus如何实现文件上传

使用elementplus 中的组件 el-upload

以上传单张图片为例:

 使用el-upload

<el-upload
    ref="uploadFile"
    action='#'
    :http-request="uploadAction"
    :on-exceed="handleExceed"
    :limit="1"
    :show-file-list="false"
    :auto-upload="true"
    :before-upload="beforeUpload" style="width: 100%;" class="left-view">
    <img v-if="bannerUrl" :src="bannerUrl" class="avatar" />
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
</el-upload>

 ts实现

<script lang="ts">
import { defineComponent, reactive, toRefs, ref } from 'vue';
import type { UploadRawFile, UploadFile, FormInstance, Action } from 'element-plus';
import { ElMessage, ElMessageBox } from 'element-plus';
import { uploadImage } from '../../apis/project';
export default defineComponent({
    name: 'BanaerManage',
    components: {},
    setup() {
        const uploadFile = ref();
        const state = reactive({
            bannerUrl: '',
            fileList: [],
            fileName: '',
        })

        const handleExceed = (files: File[], fileList: UploadFile[]) => {
            if (state.fileList.length >= 1) {
                ElMessage.error('只能上传一个图片')
                return;
            }
        }

        const beforeUpload = (file: UploadRawFile) => {
            const type = ['image/jpeg', 'image/jpg', 'image/png']
            if (type.indexOf(file.type) === -1) {
                ElMessage.error('上传的文件必须是JPG、JPEG、PNG三种之一!')
                return false
            } else if (file.size / 1024 / 1024 > 2) {
                ElMessage.error('图片大小不能超过2MB!')
                return false
            }
            return true
        }

        const uploadAction = (option: any) => {
            let param = new FormData();
            param.append('file', option.file);
            uploadImage(param).then((res: any) => {
                if (res && res.code === 0) {
                    state.fileName = res.data.fileName;
                    state.bannerUrl = res.data.imageUrl;
                    ElMessage.success('图片上传成功!');
                } else {
                    if (res && res.msg) {
                        ElMessage.error(res.msg);
                    } else {
                        ElMessage.error('图片上传失败!');
                    }
                }
            })
        }

        return {
            ...toRefs(),
            uploadFile,
            uploadAction,
            beforeUpload,
            handleExceed
        }
    }
})

简单实现场景,服务端我是用nodejs实现的,后面给大家更新一下nodejs+koa2实现如何接收文件

谢谢

  • 9
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

雪山上的小灰熊

谢谢支持哦

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

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

打赏作者

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

抵扣说明:

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

余额充值