Vue3·Uploader 文件上传

上传文件有上传组件和主文件,此文章用于自己记录笔记

以下是上传组件

<template>

	//上传组件
    <div class="upload">
        <div class="title">上传营业执照<span class="xx">*</span></div>
        <div class="upload-img">
            <van-uploader v-model="fileFace" :after-read="faceRead" :before-delete="faceDelete" upload-text="点击上传"
                max-count="1" max-size="512000" :before-read="beforeRead" />
            <div class="limit">只能上传jpg/png文件,且不超过500kb</div>
        </div>
    </div>
</template>


<script setup>
import { ref, onMounted, watch, reactive } from 'vue'
import fileApi from '../../../api/file.api'
import registerAuthApi from '@/api/registerAuth.api'
import { auth } from "@/store/auth";
import { showToast } from 'vant';
const store = auth()

onMounted(() => {
    GetFindById()
}) 


//获取附件   APP
const GetFindById = () => {

    registerAuthApi.findByUserId(store.authInfo.userId).then(res => {

        if (res.code == 200 && res.data) {

            // 查看附件--查看已经上传提交完成的文件--项目中用于浏览
            fileApi.download(res.data.businessLicenseAttachmentId).then(ress => {  //调用相应的接口
                let info = {}
                info.objectUrl = URL.createObjectURL(ress.data);
                let file = new File([ress.data], ress.data.filename, { type: ress.data.type });

                info.file = file

                fileFace.value.push(info);

                let val = {
                    face: info
                }
                emit("OtherInfo", val);

            }).catch(e => { })

        }
    })
}


const files = ref({
    faceFiles: null
})


const fileFace = ref([]);
const faceRead = (file) => {
    files.value.faceFiles = file
};

const faceDelete = (file) => {
    fileFace.value = []

    let val = {
        face: {}
    }
    emit("OtherInfo", val);
}

const beforeRead = (file) => {		//上传之前处理图片
    if(file.size > 512000){
        showToast('上传图片尺寸过大,请上传500kb以内的图片');
        return false;
      }
      return true;
};


const emit = defineEmits(["OtherInfo"])		//传递到父组件


//监听files的变化
watch(
    () => files.value.faceFiles, (newValue) => {
        let val = {
            face: newValue
        }
        emit("OtherInfo", val);

    }, { deep: true }
);


</script>



<style lang="less" scoped>
.upload {
    text-align: center;

    .upload-img {
        margin: 0 auto;
        width: 80%;
        min-height: 120px;
        border-radius: 8px;
        border: 1px solid #E9EBEF;
        padding: 25px;

        .limit {
            color: #606266;
            font-size: 10px;
        }
    }

    .title {
        line-height: 50px;
        margin-top: 8px;

        .xx {
            color: #F56C6C;
            margin-left: 4px;
        }
    }
}
</style>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用 vue-simple-uploader 插件来实现 Vue3 中的大文件上传功能。下面是一个简单的示例代码: 首先,安装 vue-simple-uploader 插件: ```bash npm install vue-simple-uploader --save ``` 然后,在需要使用上传功能的组件中引入插件并配置: ```javascript import Vue from 'vue'; import VueSimpleUploader from 'vue-simple-uploader'; Vue.use(VueSimpleUploader, { chunkSize: 1024 * 1024, // 设置分片大小,默认为1MB concurrentUploads: 3, // 设置并发上传数,默认为3 retryCount: 3, // 设置上传重试次数,默认为0 }); export default { // ... } ``` 接下来,你可以在组件中使用 `<vue-simple-uploader>` 标签来实现大文件上传: ```html <template> <div> <vue-simple-uploader ref="uploader" :url="uploadUrl" @file-added="handleFileAdded" @file-progress="handleFileProgress" @file-success="handleFileSuccess" @file-error="handleFileError" @upload-started="handleUploadStarted" @upload-completed="handleUploadCompleted" > <button @click="startUpload">开始上传</button> </vue-simple-uploader> </div> </template> <script> export default { data() { return { uploadUrl: 'http://your-upload-url', // 设置上传接口地址 }; }, methods: { handleFileAdded(file) { console.log('文件添加成功:', file); }, handleFileProgress(file, progress) { console.log('上传进度:', progress); }, handleFileSuccess(file, response) { console.log('上传成功:', response); }, handleFileError(file, error) { console.log('上传失败:', error); }, handleUploadStarted() { console.log('上传开始'); }, handleUploadCompleted() { console.log('上传完成'); }, startUpload() { this.$refs.uploader.upload(); // 调用上传方法 }, }, }; </script> ``` 以上代码是一个简单的大文件上传示例,你可以根据需要自定义处理文件上传的各个事件。参考文档和示例代码以便更好地理解和使用 vue-simple-uploader 插件:https://github.com/simple-uploader/vue-simple-uploader

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

海鸥两三

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

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

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

打赏作者

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

抵扣说明:

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

余额充值